Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created May 17, 2011 09:11
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 sobstel/976185 to your computer and use it in GitHub Desktop.
Save sobstel/976185 to your computer and use it in GitHub Desktop.
Accessing private members of the same object type
<?php
class Test
{
private $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
private function bar()
{
echo 'Accessed the private method.';
}
public function baz(Test $other)
{
// We can change the private property:
$other->foo = 'hello';
var_dump($other->foo);
// We can also call the private method:
$other->bar();
}
}
$test = new Test('test');
$test->baz(new Test('other'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment