mirror of
https://github.com/php/php-src.git
synced 2025-01-04 09:53:52 +08:00
Language Tests: returnByReference
This commit is contained in:
parent
0168fc5dda
commit
3cb550d1cd
20
tests/lang/returnByReference.001.phpt
Normal file
20
tests/lang/returnByReference.001.phpt
Normal file
@ -0,0 +1,20 @@
|
||||
--TEST--
|
||||
Returning a reference from a function
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
function &returnByRef(&$arg1)
|
||||
{
|
||||
return $arg1;
|
||||
}
|
||||
|
||||
$a = 7;
|
||||
$b =& returnByRef($a);
|
||||
var_dump($b);
|
||||
$a++;
|
||||
var_dump($b);
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
int(7)
|
||||
int(8)
|
30
tests/lang/returnByReference.002.phpt
Normal file
30
tests/lang/returnByReference.002.phpt
Normal file
@ -0,0 +1,30 @@
|
||||
--TEST--
|
||||
Returning a reference from a function.
|
||||
--FILE--
|
||||
<?php
|
||||
function &returnRef() {
|
||||
global $a;
|
||||
return $a;
|
||||
}
|
||||
|
||||
function returnVal() {
|
||||
global $a;
|
||||
return $a;
|
||||
}
|
||||
|
||||
$a = "original";
|
||||
$b =& returnVal();
|
||||
$b = "changed";
|
||||
var_dump($a); //expecting warning + "original"
|
||||
|
||||
$a = "original";
|
||||
$b =& returnRef();
|
||||
$b = "changed";
|
||||
var_dump($a); //expecting "changed"
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Strict Standards: Only variables should be assigned by reference in %s on line 13
|
||||
unicode(8) "original"
|
||||
unicode(7) "changed"
|
||||
|
55
tests/lang/returnByReference.003.phpt
Normal file
55
tests/lang/returnByReference.003.phpt
Normal file
@ -0,0 +1,55 @@
|
||||
--TEST--
|
||||
Returning a reference from a function
|
||||
--FILE--
|
||||
<?php
|
||||
function returnConstantByValue() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
function &returnConstantByRef() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
function &returnVariableByRef() {
|
||||
return $GLOBALS['a'];
|
||||
}
|
||||
|
||||
echo "\n---> 1. Trying to assign by reference the return value of a function that returns by value:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &returnConstantByValue();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &returnConstantByRef();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 3. Trying to assign by reference the return value of a function that returns by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &returnVariableByRef();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
---> 1. Trying to assign by reference the return value of a function that returns by value:
|
||||
|
||||
Strict Standards: Only variables should be assigned by reference in %s on line 17
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 7
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 3. Trying to assign by reference the return value of a function that returns by ref:
|
||||
int(5)
|
||||
int(5)
|
57
tests/lang/returnByReference.004.phpt
Normal file
57
tests/lang/returnByReference.004.phpt
Normal file
@ -0,0 +1,57 @@
|
||||
--TEST--
|
||||
Returning a reference from a static method
|
||||
--FILE--
|
||||
<?php
|
||||
Class C {
|
||||
static function returnConstantByValue() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
static function &returnConstantByRef() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
static function &returnVariableByRef() {
|
||||
return $GLOBALS['a'];
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n---> 1. Trying to assign by reference the return value of a function that returns by value:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &C::returnConstantByValue();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &C::returnConstantByRef();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 3. Trying to assign by reference the return value of a function that returns by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &C::returnVariableByRef();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
---> 1. Trying to assign by reference the return value of a function that returns by value:
|
||||
|
||||
Strict Standards: Only variables should be assigned by reference in %s on line 19
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 8
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 3. Trying to assign by reference the return value of a function that returns by ref:
|
||||
int(5)
|
||||
int(5)
|
58
tests/lang/returnByReference.005.phpt
Normal file
58
tests/lang/returnByReference.005.phpt
Normal file
@ -0,0 +1,58 @@
|
||||
--TEST--
|
||||
Returning a reference from a method
|
||||
--FILE--
|
||||
<?php
|
||||
Class C {
|
||||
function returnConstantByValue() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
function &returnConstantByRef() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
static function &returnVariableByRef() {
|
||||
return $GLOBALS['a'];
|
||||
}
|
||||
}
|
||||
$c = new C;
|
||||
|
||||
echo "\n---> 1. Trying to assign by reference the return value of a function that returns by value:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &$c->returnConstantByValue();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &$c->returnConstantByRef();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 3. Trying to assign by reference the return value of a function that returns by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &$c->returnVariableByRef();
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
---> 1. Trying to assign by reference the return value of a function that returns by value:
|
||||
|
||||
Strict Standards: Only variables should be assigned by reference in %s on line 20
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 8
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 3. Trying to assign by reference the return value of a function that returns by ref:
|
||||
int(5)
|
||||
int(5)
|
60
tests/lang/returnByReference.006.phpt
Normal file
60
tests/lang/returnByReference.006.phpt
Normal file
@ -0,0 +1,60 @@
|
||||
--TEST--
|
||||
Returning a reference from a function via another function
|
||||
--INI--
|
||||
error_reporting = E_ALL & ~E_STRICT
|
||||
--FILE--
|
||||
<?php
|
||||
function returnConstantByValue() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
function &returnConstantByRef() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
function &returnVariableByRef() {
|
||||
return $GLOBALS['a'];
|
||||
}
|
||||
|
||||
function &returnFunctionCallByRef($functionToCall) {
|
||||
return $functionToCall();
|
||||
}
|
||||
|
||||
echo "\n---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &returnFunctionCallByRef('returnConstantByValue');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &returnFunctionCallByRef('returnConstantByRef');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &returnFunctionCallByRef('returnVariableByRef');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 15
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 7
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:
|
||||
int(5)
|
||||
int(5)
|
63
tests/lang/returnByReference.007.phpt
Normal file
63
tests/lang/returnByReference.007.phpt
Normal file
@ -0,0 +1,63 @@
|
||||
--TEST--
|
||||
Returning a reference from a static method via another static method
|
||||
--INI--
|
||||
error_reporting = E_ALL & ~E_STRICT
|
||||
--FILE--
|
||||
<?php
|
||||
class C {
|
||||
static function returnConstantByValue() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
static function &returnConstantByRef() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
static function &returnVariableByRef() {
|
||||
return $GLOBALS['a'];
|
||||
}
|
||||
|
||||
static function &returnFunctionCallByRef($functionToCall) {
|
||||
return C::$functionToCall();
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &C::returnFunctionCallByRef('returnConstantByValue');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &C::returnFunctionCallByRef('returnConstantByRef');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &C::returnFunctionCallByRef('returnVariableByRef');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 16
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 8
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:
|
||||
int(5)
|
||||
int(5)
|
64
tests/lang/returnByReference.008.phpt
Normal file
64
tests/lang/returnByReference.008.phpt
Normal file
@ -0,0 +1,64 @@
|
||||
--TEST--
|
||||
Returning a reference from a non-static method via another non-static method
|
||||
--INI--
|
||||
error_reporting = E_ALL & ~E_STRICT
|
||||
--FILE--
|
||||
<?php
|
||||
class C {
|
||||
function returnConstantByValue() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
function &returnConstantByRef() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
function &returnVariableByRef() {
|
||||
return $GLOBALS['a'];
|
||||
}
|
||||
|
||||
function &returnFunctionCallByRef($functionToCall) {
|
||||
return $this->$functionToCall();
|
||||
}
|
||||
}
|
||||
$c = new C;
|
||||
|
||||
echo "\n---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &$c->returnFunctionCallByRef('returnConstantByValue');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &$c->returnFunctionCallByRef('returnConstantByRef');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
echo "\n---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\n";
|
||||
unset($a, $b);
|
||||
$a = 4;
|
||||
$b = &$c->returnFunctionCallByRef('returnVariableByRef');
|
||||
$a++;
|
||||
var_dump($a, $b);
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 16
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line 8
|
||||
int(5)
|
||||
int(100)
|
||||
|
||||
---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:
|
||||
int(5)
|
||||
int(5)
|
39
tests/lang/returnByReference.009.phpt
Normal file
39
tests/lang/returnByReference.009.phpt
Normal file
@ -0,0 +1,39 @@
|
||||
--TEST--
|
||||
Returning a references returned by another function
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
|
||||
function &returnVarByRef () {
|
||||
$b=1;
|
||||
return $b;
|
||||
}
|
||||
|
||||
function &testReturnVarByRef() {
|
||||
return returnVarByRef();
|
||||
}
|
||||
|
||||
function returnVal () {
|
||||
return 1;
|
||||
}
|
||||
|
||||
function &testReturnValByRef() {
|
||||
return returnVal();
|
||||
}
|
||||
|
||||
echo "\n---> 1. Return a variable by reference -> No warning:\n";
|
||||
|
||||
var_dump (testReturnVarByRef());
|
||||
|
||||
echo "\n---> 2. Return a value by reference -> Warning:\n";
|
||||
|
||||
var_dump (testReturnValByRef());
|
||||
|
||||
--EXPECTF--
|
||||
---> 1. Return a variable by reference -> No warning:
|
||||
int(1)
|
||||
|
||||
---> 2. Return a value by reference -> Warning:
|
||||
|
||||
Notice: Only variable references should be returned by reference in %s on line %d
|
||||
int(1)
|
Loading…
Reference in New Issue
Block a user