Created
March 24, 2011 17:14
-
-
Save mathiasverraes/885448 to your computer and use it in GitHub Desktop.
Access private properties of objects of the same class
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 | |
class Foo | |
{ | |
private $private; | |
public function __construct($value) | |
{ | |
$this->private = $value; | |
} | |
public function getOther(Foo $object) | |
{ | |
return $object->private; | |
} | |
} | |
$foo1 = new Foo('foo1'); | |
$foo2 = new Foo('foo2'); | |
echo $foo1->getOther($foo2); // outputs 'foo2' |
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 | |
class Foo | |
{ | |
// ... | |
public function equals(Foo $other) | |
{ | |
return $this->private === $other->private; | |
} | |
} | |
// ... | |
echo $foo1->equals($foo2) ? 'Equal' : 'Different'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment