print "MyClass::printHello() " . $this->protected; print "MyClass::printHello() " . $this->protected2; } } class MyClass2 extends MyClass { protected $protected = "MyClass2::protected!\n"; function printHello() { MyClass::printHello(); print "MyClass2::printHello() " . $this->public; print "MyClass2::printHello() " . $this->protected; print "MyClass2::printHello() " . $this->protected2; /* Will result in a Fatal Error: */ //print "MyClass2::printHello() " . $this->private; /* Fatal Error */ } } $obj = new MyClass(); print "Main:: " . $obj->public; //print $obj->private; /* Fatal Error */ //print $obj->protected; /* Fatal Error */ //print $obj->protected2; /* Fatal Error */ $obj->printHello(); /* Should print */ $obj2 = new MyClass2(); print "Main:: " . $obj2->private; /* Undefined */ //print $obj2->protected; /* Fatal Error */ //print $obj2->protected2; /* Fatal Error */ $obj2->printHello();
print "MyClass2::printHello() " . $this->Hello; /* Shouldn't print out anything */ print "MyClass2::printHello() " . $this->Bar; /* Shouldn't print (not declared)*/ print "MyClass2::printHello() " . $this->Foo; /* Should print */ } public function aPublicMethod() { echo "Bar::aPublicMethod() called.\n"; $this->aProtectedMethod(); } } $obj = new MyClass(); print $obj->Hello; /* Shouldn't print out anything */ print $obj->Bar; /* Shouldn't print out anything */ print $obj->Foo; /* Shouldn't print out anything */ $obj->printHello(); /* Should print */ $obj = new MyClass2(); print $obj->Hello; /* Shouldn't print out anything */ print $obj->Bar; /* Shouldn't print out anything */ print $obj->Foo; /* Shouldn't print out anything */ $obj->printHello();