[development] &$node in nodeapi

larry at garfieldtech.com larry at garfieldtech.com
Mon Oct 4 21:09:13 UTC 2010


On 10/4/10 4:01 PM, Pierre Rineau wrote:
> On Mon, 2010-10-04 at 19:53 +0200, luca capra wrote:
>>
>> The $op presave was my problem.
>>
>> This is a syntax error
>>     my_fill_cck(&$node );
>>
>> and this should be my hook:
>>
>> function mymod_nodeapi($node ...
>>
>> because&$node is a pointer, and what I need is a reference (that is how
>> is passed an object ). It's right ?
>> Interesting, I learnt something new :)
>>
>> Regards,
>> Luca
>
> With PHP 4, objects where like any other values, copied when passed as
> function parameter, which means any modification on the local copy
> wouldn't be visible within the caller function. With PHP 5, you don't
> have to add the&  operator to force the argument to pass by reference
> because objects are always references (as we could see in other
> languages such as python or java, or such).

Technicality: Without the &, you are passing by value.  Always.  The 
difference is that in PHP 5 what you are passing is a handle to an 
object rather than the object itself (not to be confused with a pointer 
as one sees in C/C++, as those don't exist in PHP).  So there's one 
object, but two variables of type "handle to get an object" that both 
point to the same object.

It's a very subtle difference that only bites you occasionally, but when 
it does it bites hard. :-)

The net result is the same: you need &$node to be compatible with PHP 4, 
but as of PHP 5 / Drupal 7 you don't want it anymore.

--Larry Garfield


More information about the development mailing list