Created
November 12, 2010 17:54
-
-
Save pkriete/674433 to your computer and use it in GitHub Desktop.
Same class, separate instances, can change sibling object's private variables. What the hell, PHP?
This file contains hidden or 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 | |
ini_set('display_errors', true); | |
class Car | |
{ | |
private $make; | |
public function __construct($make) | |
{ | |
$this->make = $make; | |
} | |
public function __toString() | |
{ | |
return $this->make; | |
} | |
public function eat(Car $c) | |
{ | |
$c->make = 'Crushed '.$c->make; | |
} | |
} | |
$weaksauce = new Car('Nissan Juke'); | |
$truck = new Car('Monster Truck'); | |
echo 'Weaksauce: '.$weaksauce."\n"; | |
$truck->eat($weaksauce); | |
echo 'Weaksauce: '.$weaksauce."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hmm, yeah that's weird. I didn't know that was possible .. it shoulnd't be.