Accessing private members of the same object type
This file contains 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 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