Skip to content

Instantly share code, notes, and snippets.

@jedgell
Last active August 29, 2015 14:11
Show Gist options
  • Save jedgell/4ce3113660855c2a475b to your computer and use it in GitHub Desktop.
Save jedgell/4ce3113660855c2a475b to your computer and use it in GitHub Desktop.
Thought experiments and lab experiments should be kept in different places...
<?php
/**
* @file
* A thought experiment.
*/
class MyFakeThing {
}
// This is ... well, nothing special.
$original = new MyFakeThing();
// Make a copy. Just be sure to take it out of the closet and feed it regularly.
// We'll use it later on as a "snapshot" to verify against.
$clone = clone $original;
// No one cares about the second clone. I remember Dolly, but what came after?
$clone_two = clone $original;
// See, at this point, you wouldn't care, except that there's a comment here!
$clone_three = clone $original;
$clone_four = clone $original;
echo "testing that everything is the 'same'" . PHP_EOL;
$test['one'] = ($original == $clone);
$test['two'] = ($original == $clone_two);
$test['three'] = ($original == $clone_three);
$test['four'] = ($clone == $clone_two);
$test['five'] = ($clone == $clone_three);
$test['six'] = ($clone_two == $clone_three);
$test['seven'] = ($clone_four == $original);
$test['eight'] = ($clone_four == $clone_two);
$test['nine'] = ($clone_four == $clone_three);
var_dump(array_sum($test) == count($test));
/**
* Make alterations to an object for investigative purposes.
*
* @param object $object
* The subject of our investigation. Pun totally intended.
*
* @return object
* Are we looking at copy or an altered original. Who knows?!
*/
function alter_original($object) {
$object->property_one = 1;
return $object;
}
/**
* Make yet more alterations to an object for investigative purposes.
*
* @param object $object
* The subject of our investigation. Again, pun totally intended.
*
* @return object
* Are we looking at copy or an altered original. Who knows?!
*/
function alter_original_further($object) {
$object->property_two = 2;
return $object;
}
/**
* Make idiotic alterations to an object for investigative purposes.
*
* @param object $object
* The subject of our investigation. Pun still intended, but regretted.
*
* @return object
* Are we looking at copy or an altered original. Who knows?!
*/
function alter_original_obscenely($object) {
$lab_rat = alter_original($object);
$lab_rat = alter_original_further($lab_rat);
// Snip off the tail, tag the ear...or something equally identifying. One
// would assume that a "copy" of $object ($lab_rat) wouldn't be affected by
// an alteration to the "original" from the argument. Let's see!
$object->property_three = 3;
return $lab_rat;
}
/**
* Make idiotic alterations to an object for investigative purposes.
*
* @param object $object
* The subject of our investigation. Pun still intended, but regretted.
*
* @return object
* Are we looking at copy or an altered original. Who knows?!
*/
function frankenstein_it(&$object) {
$chimp = $object;
$frank = alter_original($chimp);
unset($object->property_one);
$monster = &alter_original_further($frank);
// Doctor! Put down the needle and thread, that's enough, now!
// Move the object from one variable to the next, over and over again and toss
// in a reference marker to make sure.
$object->property_three = 3;
return $monster;
}
echo "the original:" . PHP_EOL;
var_dump($original);
echo "alterations being made..." . PHP_EOL;
$expected_to_be_a_copy = alter_original($original);
var_dump($expected_to_be_a_copy);
echo "and now we find out if the original has been altered..." . PHP_EOL;
var_dump($original);
echo "testing anew that \$original == \$clone:" . PHP_EOL;
var_dump($original == $clone);
echo "...same things happen with running alter_original_further...skip to obscenities." . PHP_EOL;
$guinea_pig = alter_original_obscenely($clone_two);
echo "guinea pigs are cuter than lab rats, but not much, really:" . PHP_EOL;
var_dump($guinea_pig);
echo "for comparison, a peek at the clone no one cares about:" . PHP_EOL;
var_dump($clone_two);
echo "now, I think we all know what's going to happ...well, yeah, that makes sense, too:" . PHP_EOL;
var_dump($guinea_pig == $clone_two);
echo "Monster mash ... ok ... sorry - by now you probably understand the only reason they're here is to guide the eye, right?" . PHP_EOL;
$pa = frankenstein_it($clone_four);
var_dump($pa);
echo "And here...what's going on with 4?" . PHP_EOL;
var_dump($clone_four);
echo "The only way to separated a 'copy' from the original seems to be by forcing a refer...crap...clone." . PHP_EOL;
var_dump($clone_four == $pa);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment