Created
December 25, 2013 17:54
-
-
Save ozh/8125334 to your computer and use it in GitHub Desktop.
PHP5 objects are passed by reference -- for dummies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @rmccue -- https://twitter.com/ozh/statuses/415899450809122816