Skip to content

Instantly share code, notes, and snippets.

@pkriete
Created November 12, 2010 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkriete/674433 to your computer and use it in GitHub Desktop.
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?
<?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";
@bjornbjorn
Copy link

hmm, yeah that's weird. I didn't know that was possible .. it shoulnd't be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment