Language Tests: returnByReference

This commit is contained in:
Robert Nicholson 2009-05-27 22:33:25 +00:00
parent 0168fc5dda
commit 3cb550d1cd
9 changed files with 446 additions and 0 deletions

View 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)

View 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"

View 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)

View 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)

View 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)

View 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)

View 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)

View 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)

View 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)