Skip to content

Instantly share code, notes, and snippets.

@jeanpimentel
Created July 6, 2012 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanpimentel/3060361 to your computer and use it in GitHub Desktop.
Save jeanpimentel/3060361 to your computer and use it in GitHub Desktop.
Strange behavior in PHP
<?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