]> granicus.if.org Git - php/commitdiff
Language Tests: returnByReference
authorRobert Nicholson <nicholsr@php.net>
Wed, 27 May 2009 22:34:25 +0000 (22:34 +0000)
committerRobert Nicholson <nicholsr@php.net>
Wed, 27 May 2009 22:34:25 +0000 (22:34 +0000)
tests/lang/returnByReference.001.phpt [new file with mode: 0644]
tests/lang/returnByReference.002.phpt [new file with mode: 0644]
tests/lang/returnByReference.003.phpt [new file with mode: 0644]
tests/lang/returnByReference.004.phpt [new file with mode: 0644]
tests/lang/returnByReference.005.phpt [new file with mode: 0644]
tests/lang/returnByReference.006.phpt [new file with mode: 0644]
tests/lang/returnByReference.007.phpt [new file with mode: 0644]
tests/lang/returnByReference.008.phpt [new file with mode: 0644]
tests/lang/returnByReference.009.phpt [new file with mode: 0644]

diff --git a/tests/lang/returnByReference.001.phpt b/tests/lang/returnByReference.001.phpt
new file mode 100644 (file)
index 0000000..4abc3c7
--- /dev/null
@@ -0,0 +1,20 @@
+--TEST--\r
+Returning a reference from a function\r
+--FILE--\r
+<?php\r
+\r
+function &returnByRef(&$arg1)\r
+{\r
+       return $arg1;\r
+}\r
+\r
+$a = 7;\r
+$b =& returnByRef($a);\r
+var_dump($b);\r
+$a++;\r
+var_dump($b);\r
+\r
+?>\r
+--EXPECT--\r
+int(7)\r
+int(8)
\ No newline at end of file
diff --git a/tests/lang/returnByReference.002.phpt b/tests/lang/returnByReference.002.phpt
new file mode 100644 (file)
index 0000000..3494eb5
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--\r
+Returning a reference from a function.\r
+--FILE--\r
+<?php\r
+function &returnRef() {\r
+               global $a;\r
+               return $a;\r
+}\r
+\r
+function returnVal() {\r
+               global $a;\r
+               return $a;\r
+}\r
+\r
+$a = "original";\r
+$b =& returnVal();\r
+$b = "changed";\r
+var_dump($a); //expecting warning + "original" \r
+\r
+$a = "original";\r
+$b =& returnRef();\r
+$b = "changed";\r
+var_dump($a); //expecting "changed" \r
+?>\r
+--EXPECTF--\r
+\r
+Strict Standards: Only variables should be assigned by reference in %s on line 13\r
+string(8) "original"\r
+string(7) "changed"\r
diff --git a/tests/lang/returnByReference.003.phpt b/tests/lang/returnByReference.003.phpt
new file mode 100644 (file)
index 0000000..04a6338
--- /dev/null
@@ -0,0 +1,55 @@
+--TEST--\r
+Returning a reference from a function\r
+--FILE--\r
+<?php\r
+function returnConstantByValue() {\r
+       return 100;\r
+}\r
+\r
+function &returnConstantByRef() {\r
+       return 100;\r
+}\r
+\r
+function &returnVariableByRef() {\r
+       return $GLOBALS['a'];\r
+}\r
+\r
+echo "\n---> 1. Trying to assign by reference the return value of a function that returns by value:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &returnConstantByValue();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &returnConstantByRef();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 3. Trying to assign by reference the return value of a function that returns by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &returnVariableByRef();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+?>\r
+--EXPECTF--\r
+\r
+---> 1. Trying to assign by reference the return value of a function that returns by value:\r
+\r
+Strict Standards: Only variables should be assigned by reference in %s on line 17\r
+int(5)\r
+int(100)\r
+\r
+---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 7\r
+int(5)\r
+int(100)\r
+\r
+---> 3. Trying to assign by reference the return value of a function that returns by ref:\r
+int(5)\r
+int(5)\r
diff --git a/tests/lang/returnByReference.004.phpt b/tests/lang/returnByReference.004.phpt
new file mode 100644 (file)
index 0000000..f185c12
--- /dev/null
@@ -0,0 +1,57 @@
+--TEST--\r
+Returning a reference from a static method\r
+--FILE--\r
+<?php\r
+Class C {\r
+       static function returnConstantByValue() {\r
+               return 100;\r
+       }\r
+\r
+       static function &returnConstantByRef() {\r
+               return 100;\r
+       }\r
+       \r
+       static function &returnVariableByRef() {\r
+               return $GLOBALS['a'];\r
+       }\r
+}\r
+\r
+echo "\n---> 1. Trying to assign by reference the return value of a function that returns by value:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &C::returnConstantByValue();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &C::returnConstantByRef();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 3. Trying to assign by reference the return value of a function that returns by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &C::returnVariableByRef();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+?>\r
+--EXPECTF--\r
+\r
+---> 1. Trying to assign by reference the return value of a function that returns by value:\r
+\r
+Strict Standards: Only variables should be assigned by reference in %s on line 19\r
+int(5)\r
+int(100)\r
+\r
+---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 8\r
+int(5)\r
+int(100)\r
+\r
+---> 3. Trying to assign by reference the return value of a function that returns by ref:\r
+int(5)\r
+int(5)\r
diff --git a/tests/lang/returnByReference.005.phpt b/tests/lang/returnByReference.005.phpt
new file mode 100644 (file)
index 0000000..a7b6da2
--- /dev/null
@@ -0,0 +1,58 @@
+--TEST--\r
+Returning a reference from a method\r
+--FILE--\r
+<?php\r
+Class C {\r
+       function returnConstantByValue() {\r
+               return 100;\r
+       }\r
+       \r
+       function &returnConstantByRef() {\r
+               return 100;\r
+       }\r
+       \r
+       static function &returnVariableByRef() {\r
+               return $GLOBALS['a'];\r
+       }\r
+}\r
+$c = new C;\r
+\r
+echo "\n---> 1. Trying to assign by reference the return value of a function that returns by value:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &$c->returnConstantByValue();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &$c->returnConstantByRef();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 3. Trying to assign by reference the return value of a function that returns by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &$c->returnVariableByRef();\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+?>\r
+--EXPECTF--\r
+\r
+---> 1. Trying to assign by reference the return value of a function that returns by value:\r
+\r
+Strict Standards: Only variables should be assigned by reference in %s on line 20\r
+int(5)\r
+int(100)\r
+\r
+---> 2. Trying to assign by reference the return value of a function that returns a constant by ref:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 8\r
+int(5)\r
+int(100)\r
+\r
+---> 3. Trying to assign by reference the return value of a function that returns by ref:\r
+int(5)\r
+int(5)\r
diff --git a/tests/lang/returnByReference.006.phpt b/tests/lang/returnByReference.006.phpt
new file mode 100644 (file)
index 0000000..4019f3a
--- /dev/null
@@ -0,0 +1,60 @@
+--TEST--\r
+Returning a reference from a function via another function\r
+--INI--\r
+error_reporting = E_ALL & ~E_STRICT\r
+--FILE--\r
+<?php\r
+function returnConstantByValue() {\r
+       return 100;\r
+}\r
+\r
+function &returnConstantByRef() {\r
+       return 100;\r
+}\r
+\r
+function &returnVariableByRef() {\r
+       return $GLOBALS['a'];\r
+}\r
+\r
+function &returnFunctionCallByRef($functionToCall) {\r
+       return $functionToCall();\r
+}\r
+\r
+echo "\n---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &returnFunctionCallByRef('returnConstantByValue');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+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";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &returnFunctionCallByRef('returnConstantByRef');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &returnFunctionCallByRef('returnVariableByRef');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+?>\r
+--EXPECTF--\r
+---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 15\r
+int(5)\r
+int(100)\r
+\r
+---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 7\r
+int(5)\r
+int(100)\r
+\r
+---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\r
+int(5)\r
+int(5)\r
diff --git a/tests/lang/returnByReference.007.phpt b/tests/lang/returnByReference.007.phpt
new file mode 100644 (file)
index 0000000..1247e55
--- /dev/null
@@ -0,0 +1,63 @@
+--TEST--\r
+Returning a reference from a static method via another static method\r
+--INI--\r
+error_reporting = E_ALL & ~E_STRICT\r
+--FILE--\r
+<?php\r
+class C {\r
+       static function returnConstantByValue() {\r
+               return 100;\r
+       }\r
+       \r
+       static function &returnConstantByRef() {\r
+               return 100;\r
+       }\r
+       \r
+       static function &returnVariableByRef() {\r
+               return $GLOBALS['a'];\r
+       }\r
+       \r
+       static function &returnFunctionCallByRef($functionToCall) {\r
+               return C::$functionToCall();\r
+       }\r
+}\r
+\r
+echo "\n---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &C::returnFunctionCallByRef('returnConstantByValue');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+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";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &C::returnFunctionCallByRef('returnConstantByRef');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &C::returnFunctionCallByRef('returnVariableByRef');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+?>\r
+--EXPECTF--\r
+\r
+---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 16\r
+int(5)\r
+int(100)\r
+\r
+---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 8\r
+int(5)\r
+int(100)\r
+\r
+---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\r
+int(5)\r
+int(5)
\ No newline at end of file
diff --git a/tests/lang/returnByReference.008.phpt b/tests/lang/returnByReference.008.phpt
new file mode 100644 (file)
index 0000000..d595de2
--- /dev/null
@@ -0,0 +1,64 @@
+--TEST--\r
+Returning a reference from a non-static method via another non-static method\r
+--INI--\r
+error_reporting = E_ALL & ~E_STRICT\r
+--FILE--\r
+<?php\r
+class C {\r
+       function returnConstantByValue() {\r
+               return 100;\r
+       }\r
+       \r
+       function &returnConstantByRef() {\r
+               return 100;\r
+       }\r
+       \r
+       function &returnVariableByRef() {\r
+               return $GLOBALS['a'];\r
+       }\r
+       \r
+       function &returnFunctionCallByRef($functionToCall) {\r
+               return $this->$functionToCall();\r
+       }\r
+}\r
+$c = new C;\r
+\r
+echo "\n---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &$c->returnFunctionCallByRef('returnConstantByValue');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+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";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &$c->returnFunctionCallByRef('returnConstantByRef');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+echo "\n---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\n";\r
+unset($a, $b);\r
+$a = 4;\r
+$b = &$c->returnFunctionCallByRef('returnVariableByRef');\r
+$a++;\r
+var_dump($a, $b);\r
+\r
+?>\r
+--EXPECTF--\r
+\r
+---> 1. Via a return by ref function call, assign by reference the return value of a function that returns by value:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 16\r
+int(5)\r
+int(100)\r
+\r
+---> 2. Via a return by ref function call, assign by reference the return value of a function that returns a constant by ref:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line 8\r
+int(5)\r
+int(100)\r
+\r
+---> 3. Via a return by ref function call, assign by reference the return value of a function that returns by ref:\r
+int(5)\r
+int(5)
\ No newline at end of file
diff --git a/tests/lang/returnByReference.009.phpt b/tests/lang/returnByReference.009.phpt
new file mode 100644 (file)
index 0000000..6320375
--- /dev/null
@@ -0,0 +1,39 @@
+--TEST--\r
+Returning a references returned by another function\r
+--FILE--\r
+<?php\r
+\r
+\r
+function &returnVarByRef () {\r
+    $b=1;\r
+       return $b; \r
+}\r
+\r
+function &testReturnVarByRef() {\r
+       return returnVarByRef();\r
+}\r
+\r
+function returnVal () {\r
+return 1; \r
+}\r
+\r
+function &testReturnValByRef() {\r
+       return returnVal();\r
+}\r
+\r
+echo "\n---> 1. Return a variable by reference -> No warning:\n";\r
+\r
+var_dump (testReturnVarByRef());\r
+\r
+echo "\n---> 2. Return a value by reference -> Warning:\n";\r
+\r
+var_dump (testReturnValByRef());\r
+\r
+--EXPECTF--\r
+---> 1. Return a variable by reference -> No warning:\r
+int(1)\r
+\r
+---> 2. Return a value by reference -> Warning:\r
+\r
+Notice: Only variable references should be returned by reference in %s on line %d\r
+int(1)\r