Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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 ajoutée par Adeel Mughal , Sr Full Stack PHP/Python Developer / TL , Gulf Business Horizon (Riyadh SA)
Date de publication: 2013/10/08
Muhammad Waqas
par Muhammad Waqas , Dot Net and MVC Developer , Osys Technologies

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

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?