Skip to content

Instantly share code, notes, and snippets.

@panvid
Created May 8, 2017 13:58
Show Gist options
  • Save panvid/5c9ab1182a7265166edd9bcdef404487 to your computer and use it in GitHub Desktop.
Save panvid/5c9ab1182a7265166edd9bcdef404487 to your computer and use it in GitHub Desktop.
<?php
class A {
public $var = "blub";
function __toString() {
return $var;
}
}
function makeBlab($object) {
$object->var = "overridden";
}
function makeAClass($object) {
$object = new A();
}
function makeArray($object) {
$object = [];
}
function makeArrayWithReference(&$object) {
$object = [];
}
$a = new A();
echo print_r($a, true) . '<br/>';
// objects will be hand over as reference, so will be manipulated in function
makeBlab($a);
echo print_r($a, true) . '<br/>';
// try to override this object with a new initialized one
makeAClass($a);
echo print_r($a, true) . '<br/>';
// try to override this object with an array
makeArray($a);
echo print_r($a, true) . '<br/>';
// try to override this object with an array. Use the values a reference
makeArrayWithReference($a);
echo print_r($a, true) . '<br/>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment