ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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
تم إضافة السؤال من قبل Adeel Mughal , Sr Full Stack PHP/Python Developer / TL , Gulf Business Horizon (Riyadh SA)
تاريخ النشر: 2013/10/08
Muhammad Waqas
من قبل 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

المزيد من الأسئلة المماثلة