Create a gist now

Instantly share code, notes, and snippets.

@cdzombak /output.txt Secret
Last active Dec 14, 2015

What would you like to do?
protected/private scope across instances in PHP (also applies to Java)
object(a)#2 (1) {
["protectedIvar":protected]=>
string(3) "bar"
}
hi, this is going to be printed by a private method
<?
class a {
protected $protectedIvar;
private function privateMethod() {
echo "hi, this is going to be printed by a private method from another instance of this class";
}
public function testA() {
$anotherA = new a();
$anotherA->privateMethod();
}
}
class b extends a {
public function test() {
$a = new a();
$a->protectedIvar = "bar";
var_dump($a);
}
}
$b = new b();
$b->test();
$a = new a();
$a->testA();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment