You are here: Re: [PHP] Tidying code for PHP5.0.5/PHP4.4.0 « PHP « IT news, forums, messages
Re: [PHP] Tidying code for PHP5.0.5/PHP4.4.0

Posted by Rasmus Lerdorf on 09/20/05 18:41

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация