Skip to content

Instantly share code, notes, and snippets.

@ozh
Created December 25, 2013 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozh/8125334 to your computer and use it in GitHub Desktop.
Save ozh/8125334 to your computer and use it in GitHub Desktop.
PHP5 objects are passed by reference -- for dummies
<?php
function change_array( $array ) {
$array[ 'x' ] = 2;
}
$bar = array();
$bar['x'] = 1;
change_array( $bar );
var_dump( $bar['x'] ); // 1 -- as expected
function change_object( $object ) {
$object->x = 2;
}
$foo = new stdClass;
$foo->x = 1;
change_object( $foo );
var_dump( $foo->x ); // 2, because, PHP5 objects passed by reference, bitches !
@ozh
Copy link
Author

ozh commented Dec 25, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment