Date: 11/23/00
- Next message: Ignacio Vazquez-Abrams: "Re: [PHP-DEV] Passing-by-reference"
- Previous message: Danny Heijl: "[PHP-DEV] Wanted : maintainer for informix driver"
- Next in thread: Ignacio Vazquez-Abrams: "Re: [PHP-DEV] Passing-by-reference"
- Reply: Ignacio Vazquez-Abrams: "Re: [PHP-DEV] Passing-by-reference"
- Maybe reply: Andi Gutmans: "Re: [PHP-DEV] Passing-by-reference"
- Reply: André Langhorst: "Re: [PHP-DEV] Passing-by-reference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I need some input from people. As you might know (or not), a few weeks ago,
Zeev and I fixed a serious memory corruption problem when sending values to
functions which expected to receive their arguments by reference when the
passed value was *not* a variable. We never quite intended this to work but
it came up...
For example the following is the behavior "as designed":
function foo(&$bar)
{
$bar = 5;
}
1) foo($obj); // Will work
2) foo($a); // Will work
3) foo(new testclass()); // Will work
4) foo(5); // Will not work
5) foo("Barbara"); // Will not work
The following was not supposed to work but behaved in an undefined
manner/corruption:
foo(expression);
as in:
6) foo(bar());
7) foo($obj->method());
8) foo("\$a = $a");
Now some people have wanted to be able to pass function return values by
reference. For example:
foo(FactoryMethod());
I created a patch which will allow this to work safely including 6) and 7)
*if and only if* the function/method is defined to return a reference. This
means that the developer needs to be smart enough to mark his function to
return a reference. The logic is that as the function which is being called
expects to receive a reference the coder should be clearly forced to return
a reference from the function.
So he'd need to define FactoryMethod() as (syntax which has existed for a
while to force returning a reference):
function &FactoryMethod()
{
}
Anyway, what my patch does is allow such defined function's to be used as
function arguments to functions which want to receive a reference. The side
effect of my patch is that there are cases where it will work when a
function returns something which isn't a reference (or a reference by
chance). This is the area which isn't quite interesting but IMO should be
documented as undefined. I think there is an option though to always allow
return values to be passed by reference *even* if they aren't really
references.
For example:
function bar()
{
$a = $GLOBALS["foobar"];
return $a;
}
foo(bar());
In this case, if I change my patch instead of getting undefined behavior
the Zend Engine would take the result of bar() and create a separate copy
of it, which it would then send to foo(). Not really a reference but just
making sure that the thing is sent.
If you want to correctly return an object from bar() and send it to foo()
you would still need to use the function &bar() notation.
So after this whole long explanation which is quite complex (and probably
not 100% clear but I'm in a rush) I am wondering:
a) Do you prefer to document the three ways which work foo($a), foo(bar())
where bar() is defined as function which returns a reference, and foo(new
testclass) and all the rest will be documented as undefined.
b) Do you want to define the "undefined" behavior in some way, i.e., always
create a copy of the returned data and pass it.
Now I'm aware that the whole reference thing is a bit confusing, mainly
because it started out as an internal Zend Engine feature (for the
executor) but ended up being used by people (with the =& operator for
example which was later introduced).
Anyway, your comments would be appreciated.
Thanks,
Andi
--- Andi Gutmans <andi <email protected>> http://www.zend.com/-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe <email protected> For additional commands, e-mail: php-dev-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>
- Next message: Ignacio Vazquez-Abrams: "Re: [PHP-DEV] Passing-by-reference"
- Previous message: Danny Heijl: "[PHP-DEV] Wanted : maintainer for informix driver"
- Next in thread: Ignacio Vazquez-Abrams: "Re: [PHP-DEV] Passing-by-reference"
- Reply: Ignacio Vazquez-Abrams: "Re: [PHP-DEV] Passing-by-reference"
- Maybe reply: Andi Gutmans: "Re: [PHP-DEV] Passing-by-reference"
- Reply: André Langhorst: "Re: [PHP-DEV] Passing-by-reference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

