[development] &$node in nodeapi

Pierre Rineau pierre.rineau at makina-corpus.com
Mon Oct 4 21:01:08 UTC 2010


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).

The reason why D6 still have the & operator is because it's meant to be
compatible with PHP 4, but it can have really ugly side effects with PHP
5 (fortunately, I still did not encounter it with other modules, it
seems that developers are for the most careful about what they are
doing). But if you have a module that unset the local pointer value or
replace it by anything else, it could have rather strange side effects.
I did experienced it with the views module somehow doing some bad typo
(views uses a lot of objects and always force the & operator everywhere
to remain compatible with PHP 4).

Pierre.




More information about the development mailing list