Is it possible to rewrite protected method like this in helpers? Show
7ochem 7,36214 gold badges47 silver badges77 bronze badges asked Dec 3, 2014 at 7:17
3
1 Answeranswered Dec 3, 2014 at 8:04
RichardRichard 1,71610 silver badges18 bronze badges 1
This is probably a basic question but im following this tutorial and at one point the code looks something like this.
However when i use this
i get this error
The problem seems to be when i add protected to the get_name() function in the employee class but it seems to me that this is the preferred way to override in the tutorial. Any ideas?
asked Oct 9, 2013 at 15:49
1 The problem isn't that you cannot override the protected method, it's that you are calling a protected method from outside of the class. After the
class is instantiated, you can call a public method which in turn could call For example:
In your example, you would probably be best making answered Oct 9, 2013 at 15:56
acairnsacairns 4854 silver badges12 bronze badges "The problem seems to be when i add answered Oct 9, 2013 at 15:52
WaldheinzWaldheinz 10.4k3 gold badges31 silver badges60 bronze badges 1 Can protected methods be overridden in PHP?Code Inspection: Method visibility should not be overridden
Overriding a protected method with a public method in a child class makes this method accessible from everywhere. This violates the encapsulation principle and is considered bad practice. See Method Visibility (php.net) for details.
What is protected method in PHP?protected - the property or method can be accessed within the class and by classes derived from that class. private - the property or method can ONLY be accessed within the class.
How can I access protected variable in PHP?Protected access modifiers cannot be applied for classes. However, they can be called by a subclass which is inherited from its parent class. Hence one can declare the required method or a variable as protected by prefixing it with a “protected” keyword.
Can protected methods be inherited?protected means access to the method is restricted to the same package or by inheritance. So the answer is, yes, protected methods can be overridden by a subclass in any package. By contrast, package (default) scoped methods are not visible even to subclasses that are in a different package.
|