Re: Tidying code for PHP5.0.5/PHP4.4.0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Michael Sims wrote:
> Jochem Maas wrote:
> 
>>>>foo($a = 5);
>>
>>by definition the expression is evaluated _before_ the function is
>>called - so the expression is not passed to the function, the result
>>of the expression is passed ... I was under the impression that the
>>the expression evaluates to a 'pointer' (I'm sure thats bad
>>terminology) to $a ... which can taken by reference by the function.
>>
>>possibly I am completely misunderstanding what goes on here.
> 
> 
> When used as an expression, an assignment evaluates to whatever is on the right side of the assignment operator, not the left.  Example:
> 
> var_dump($a = 5);
> outputs
> int(5)
> 
> var_dump($a = "some string");
> outputs
> string(11) "some string"
> 
> So, as far as foo() knows:
> 
> foo($a = 5);
> and
> foo(5);
> 
> are exactly the same...

The value passed is the same, but when passed as $a=5 then the value has
a symbol table entry associated with it whereas if you just pass in 5 it
doesn't.  That means that:

function foo(&$arg) { $arg =6; }

will work if you call: foo($a=5);
but you will get an error if you have: foo(5);

The above should be pretty straightforward and isn't new.  What was
always fuzzy was this slightly more involved example:

function foo(&$arg) { $arg =6; }
function bar() { return 5; }
foo(bar());

Here we are essentially passing 5 to foo() again, but it isn't a
constant, it is what is known as a temp_var internally in PHP.  Chances
are code like this is hiding a bug, because the temp_var is going to
drop out of scope and the change to it in foo() will be discarded.  This
was initially made to throw a fatal error in PHP 5.x, which was a
mistake on our part.  It now throws an E_STRICT instead because in some
cases this may not actually be a bug.  There are some cases where you
don't care about the discarded reference.  In PHP 4.3.x assigning
references to temp_vars could cause memory corruption which prompted the
fixes to 4.4 and the introduction of an E_NOTICE in certain cases.

-Rasmus

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux