ballhogjoni Posted October 4, 2013 Share Posted October 4, 2013 So I have an instance of UserXyzPersister which extends DBPersisterAbstract which extends DBPersisterBase. There is a function in the DBPersisterBase class that is protected: protected function getArray($arg){ ... } I'm trying to access it via: $u = new UserXyzPersister(); $u->getArray($arg); This is working. My question is, can only the children of DBPersisterBase access getArray() or can the parents as well? Quote Link to comment Share on other sites More sharing options...
Solution KevinM1 Posted October 4, 2013 Solution Share Posted October 4, 2013 (edited) So I have an instance of UserXyzPersister which extends DBPersisterAbstract which extends DBPersisterBase. There is a function in the DBPersisterBase class that is protected: protected function getArray($arg){ ... } I'm trying to access it via: $u = new UserXyzPersister(); $u->getArray($arg); This is working. My question is, can only the children of DBPersisterBase access getArray() or can the parents as well? If something is declared public or protected, then only the children of that class gain access to it. Inheritance is a one-way street. EDIT: and by children, I mean any class down the inheritance chain from the original. Try it yourself: class A { protected function hello() { echo "Hello from class A!"; } } class B extends class A { protected function test() { echo "Hello from class B!"; } } class C extends class B {} $a = new A(); $c = new C(); $c->hello(); $a->test(); Edited October 4, 2013 by KevinM1 Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 4, 2013 Share Posted October 4, 2013 It has been my experience that protected methods are available up and down the inheritance tree. <?php error_reporting(E_ALL); ini_set('display.errors', 1); class A { protected function hello () { print("Hello from A\n"); } public function askMe() { print("A says:\n"); $this->hello(); $this->test(); } } class B extends A { protected function test() { print("Hello from B\n"); } public function askMe() { print("B says:\n"); $this->hello(); $this->test(); print("Ask A ..."); parent::askMe(); } } class C extends B { public function askMe() { print("C says:\n"); $this->hello(); $this->test(); print("Ask B ..."); parent::askMe(); } } print("Version: " . phpversion() . "\n\n"); $c = new C; $c->askMe(); $c->test(); $c->hello(); /* Version: 5.3.4 C says: Hello from A Hello from B Ask B ...B says: Hello from A Hello from B Ask A ...A says: Hello from A Hello from B Fatal error: Call to protected method B::test() from context '' in D:\wwwIntranet\wwwDEV\public\inhTest.php on line 46 If you comment out line 46, then you get this error: Fatal error: Call to protected method A::hello() from context '' in D:\wwwIntranet\wwwDEV\public\inhTest.php on line 47 */ From an OOP standpoint, it doesn't make sense for A to call test() since it is not defined until B; unless of course, A defines it as abstract so we know it will be there later. You cannot call hello() or test() from outside of the class, because they are protected. I think KevinM1 is answering questions in his sleep, again. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 4, 2013 Share Posted October 4, 2013 Good call on protected vs. public. The rest still stands. Remember: the OP wasn't asking about best practice, just whether something could work or not. Quote Link to comment Share on other sites More sharing options...
vinny42 Posted October 5, 2013 Share Posted October 5, 2013 Remember: the OP wasn't asking about best practice, just whether something could work or not. Huh? are you seriously saying "don't warn him that he's about to make a mistake, because he didn't ask"? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.