Created
July 6, 2012 14:09
-
-
Save jeanpimentel/3060361 to your computer and use it in GitHub Desktop.
Strange behavior in PHP
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 C1 { | |
protected $propertyProtected = 'foo'; | |
} | |
class C2 { | |
public function test() { | |
$objC1 = new C1(); | |
echo $objC1->propertyProtected; | |
} | |
} | |
class C3 extends C2 { | |
public function test() { | |
$objC1 = new C1(); | |
echo $objC1->propertyProtected; | |
} | |
} | |
class C4 extends C1 { | |
public function test() { | |
$objC1 = new C1(); | |
echo $objC1->propertyProtected; | |
} | |
} | |
// Fatal error: Cannot access protected property | |
//$objC1 = new C1(); | |
//var_dump($objC1->propertyProtected); | |
// Fatal error: Cannot access protected property | |
// $objC2 = new C2(); | |
// $objC2->test(); | |
// Fatal error: Cannot access protected property | |
//$objC3 = new C3(); | |
//$objC3->test(); | |
// Why it works? Inside test() method I'm accessing a property in another object, | |
// not in a parent object. It isn't $this->propertyProtected; | |
// $objC4 = new C4(); | |
// $objC4->test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment