Can we override protected method in php?

Is it possible to rewrite protected method like this in helpers?

class Fishpig_Wordpress_Helper_Filter extends Fishpig_Wordpress_Helper_Abstract
{
 ...
    protected function _applyShortcodes(&$content, $object, $context) {

  • magento-1.9
  • overrides

Can we override protected method in php?

7ochem

7,36214 gold badges47 silver badges77 bronze badges

asked Dec 3, 2014 at 7:17

3

  • No, it breaks oop concept then.

    Dec 3, 2014 at 7:33

  • Iam not sure this works but once have a try by looking at this link1 and link2advice to maintain a backup.

    Dec 3, 2014 at 7:49

  • oh sorry.protected method can accessed by inherited class.

    Dec 3, 2014 at 8:18

1 Answer

answered Dec 3, 2014 at 8:04

RichardRichard

1,71610 silver badges18 bronze badges

1

  • that was i thought. thx

    Dec 3, 2014 at 8:08

This is probably a basic question but im following this tutorial and at one point the code looks something like this.

<?php

class person
{
    public $name;
    public $height;
    protected $social_security_no;
    private $pin_number = 3242;

    public function __construct($person_name)
    {
        $this->name = $person_name;
    }
    public function set_name($new_name)
    {
        $this->name = $new_name;
    }

    protected function get_name()
    {
        return $this->name;
    }

    public function get_pin_number_public()
    {
        $this->pub_pin = $this->get_pin_number();
        return $this->pub_pin;
    }

    private function get_pin_number()
    {
        return $this->pin_number;
    }

}

class employee extends person
{

    public function __construct($person_name)
    {
        $this->name = $person_name;
    }

    protected function get_name()
    {
        return $this->name;
    }
}

However when i use this

<?php include "class_lib.php";?>
    </head>
    <body id="theBody">
    <div>

<?php
$maria = new person("Default");

$dave = new employee("David Knowler");
echo $dave->get_name();
?>

i get this error

Fatal error: Call to protected method employee::get_name() from context '' in C:\Users\danny\Documents\Workspace\test\index.php on line 13

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

Can we override protected method in php?

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 get_name() and you will see that the code will work as expected.

For example:

class employee extends person {

    function __construct($person_name){
        $this->name = $person_name;
    }

    protected function get_name() {
        return $this->name;
    }

    public function name()
    {
        return $this->get_name();
    }
}

$dave = new employee("David Knowler");
echo $dave->name();

In your example, you would probably be best making get_name() public.

answered Oct 9, 2013 at 15:56

acairnsacairns

4854 silver badges12 bronze badges

"The problem seems to be when i add protected to the get_name() function in the employee class" -- this is your answer. A protected method can only be called from the very same class or subclasses, not "from the outside". Your method has to be public if you want to use it this way.

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.