Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is protected method in abstract class?

In my PHP code I have a protected method in an abstract class which we'll call class A. If I create a new class called B which extends A, do I have to simply declare it public in B or do I have to re-write all the implementation so when I instanciate B I can then call this method? abstract class A { protected function test() { //do some stuff here } } class B extends A { public function test() { //Do I need to do something here? } }

user-image
Question added by Adeel Mughal , Sr Full Stack PHP/Python Developer / TL , Gulf Business Horizon (Riyadh SA)
Date Posted: 2013/10/08
Muhammad Waqas
by Muhammad Waqas , C#/.Net Developer , Signup solution

You need to do parent::test() call - or not do not declare method in child class at all. In second case method will be inherited from parent class while in first case it will be method of B which calls parent method, i.e. method of A.

Also, if you will not declare method in child class, it will not be public, so it may be not the thing you're looking for (mentioned to show how inheritance works). I.e. if you want to have public method - the only way would be calling parent::test() from inside test() method of B class

Abdullatif Ali Abdullatif Hamid Ahmed Shareef
by Abdullatif Ali Abdullatif Hamid Ahmed Shareef , IT Supervisor , National Feed Company FEEDCO

No 

More Questions Like This