-1

when I implement the HOOK_node_presave , I can access to the $node and alter it ex:

function drupal7_test_node_presave($node){
    $node->title = 'Abricoooooooooooooooooo';
}

the strange thing is that this hook is invoked with module_invoke_all which should pass argument by value (and not by reference like drupal_alter() )

module_invoke_all('node_presave', $node);
var_dump($node->title); die();

this output : Abricoooooooooooooooooo , when the $node property has been changed ? it should not !!!!

  • 1
    The parameter passed in is a reference to the object, and thus you can change the same values of that object in memory. – mradcliffe Apr 11 at 1:45
  • 1
    To clear even more the @mradcliffe response take into account that the parameter you pass is a value, but that value is a reference to the object. That means that i n practice you are passing value by reference. In PHP objects are always passed by reference (or by value, but the value is a reference to object). Otherwise, when passing objects by value they should be copied. Copy an entire object is complex, error prone and can lead to performance problems. – sanzante Apr 13 at 9:11
1

this hook is invoked with module_invoke_all which should pass argument by value (and not by reference like drupal_alter() )

That's not how PHP works - $node is an object; objects are (ostensibly) passed by reference. Or, more accurately, from the docs

One of the key-points of PHP5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

Emphasis mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.