Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Created March 24, 2011 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mathiasverraes/885448 to your computer and use it in GitHub Desktop.
Save mathiasverraes/885448 to your computer and use it in GitHub Desktop.
Access private properties of objects of the same class
<?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'
<?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