How to handle references in PHP 4.4.0
|
valiant
Joined: 2003-01-04
Posts: 32506 |
Posted: Sun, 2005-07-17 20:17
|
|
I'm working on making G2 PHP 4.4.0 compatible. The "new" PHP rules for references are rather silly IMO, but they exist now. See: http://www.php.net/manual/en/language.references.return.php assigning by reference $example =& test();
function &test() {
$val = 'foo';
return $val;
}incorrect: $example =& test();
function test() {
$val = 'foo';
return $val;
}In the last example, the function was defined to no return by reference. A mistake that I found often in the smarty / adodb code: returning by reference $example =& test();
function &test() {
$val = 'foo';
return $val;
}correct: $example = test();
function &test() {
$val = 'foo';
return $val;
}Notice that you can assign by value or by reference, it doesn't matter. If you assign by value, it will just copy the variable. incorrect: function &test() {
return null;
}correct: function &test() {
$ret = null;
return $ret;
}incorrect:
function &test() {
return array();
}correct:
function &test() {
$ret = array();
return $ret;
}incorrect:
function &test() {
return 1;
}correct:
function &test() {
$ret = 1;
return $ret;
}incorrect:
function &test() {
return 'string';
}correct:
function &test() {
$ret = 'string';
return $ret;
}incorrect:
function &test() {
return array(GalleryStatus::success(), $foo);
}incorrect:
function &test() {
return $ret = array(GalleryStatus::success(), $foo);
}correct:
function &test() {
$ret = array(GalleryStatus::success(), $foo);
return $ret;
}incorrect:
function &test() {
return (string)$this->_somevar;
}correct:
function &test() {
settype($this->_somevar, 'string');
return $this->_somevar;
}incorrect:
function &test() {
return foo();
}
function &foo() {
$bar = 'bla";
return $bar;
}correct:
function &test() {
$ret =& foo();
return $ret;
}
function &foo() {
$bar = 'bla";
return $bar;
}
Else, you'd get this PHP notice: Summary 1. If you are assigning by reference, make sure the function on the right (if it's a function), is returning by reference. 2. If you create a function which is returning by reference, make sure all return statements in this function are of the pattern "return $varName;". Don't create new variables in the return statement and don't return constant values like null, 1, ... just return; or expressions. |
|
| Login or register to post comments |

Posts: 8600
Thanks valiant.. I see the fixes for G2 are now in cvs.
Posts: 32506
In PHP 5.1.0 some changes were corrected. Particularely, return otherFunction(); is allowed again, as long as the other function also returns-by-reference.
That means that the following code doesn't need to be changed for PHP 5.1.0+ . But since we're offering G2 for PHP 4.4.0+ too, we still can't do something like that without the above mentioned fix in G2.
function &test() { return foo(); } function &foo() { $bar = 'bla"; return $bar; }pass by reference
PHP 5.0.5+ is also more strict about passing values by reference. e.g. in PHP 4.3.11 if you did for some reason:
$myVal = array_pop(foo()); function foo() { return array('foo', 'bar', 'baz'); }you'll have it to change for PHP 5.0.5+ to