Skip to content

Instantly share code, notes, and snippets.

@jeremysells
Last active March 2, 2017 11:18
Show Gist options
  • Save jeremysells/d1697375c84af51af2f6cde80286bc97 to your computer and use it in GitHub Desktop.
Save jeremysells/d1697375c84af51af2f6cde80286bc97 to your computer and use it in GitHub Desktop.
objects pass by reference examples.php
<?php
//---TEST1----------------------------------------------------
function objectTest(stdClass $input) {
$input->name = "Sally";
}
$object = new stdClass;
$object->name = "Bob";
$object->address = "1 Test Street";
objectTest($object);
echo $object->name; //Sally
echo $object->address; //1 Test Street
//---TEST2----------------------------------------------------
function scalarTest(array $input) {
$input["name"] = "Sally";
}
$scalar = array();
$scalar["name"] = "Bob";
$scalar["address"] = "1 Test Street";
scalarTest($scalar);
echo $scalar["name"]; //Bob
echo $scalar["address"]; //1 Test Street
//---TEST3----------------------------------------------------
function objectScalarTest(array $input) {
$object2 = new StdClass;
$input[2] = $object2;
$object2->name = "Jeremy";
$object2->address = "123 Example Place";
echo $input[2]->name; //Jeremy
$input[1]->name = "Sally";
}
$scalar = array();
$object1 = new stdClass;
$object1->name = "Bob";
$object1->address = "1 Test Street";
$scalar[1] = $object1;
echo count($scalar); //1
objectScalarTest($scalar);
echo count($scalar); //1
echo $scalar[1]->name; //Sally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment