0

So I have this code, see below. I am running an error while getting a protected value. Thought that protected values will be inherited. I was figuring out why we need to call parent::__construct(), while we just can extend a class.

If you could tell me whats wrong and how can I do it the right way, that would be awesome.

    <?php 
    /**
    * DogWords
    */

    class DogWords
    {
        protected $words = array('Wrr', 'Grr', 'Weeee', 'Houou');
    }

    /**
    * Dog Class
    */
    class Dog extends DogWords
    {
        protected $dogname = NULL;
        protected $dogwords = NULL;

        public function __construct($dogname)
        {
            $this->dogname = $dogname;
            $this->dogwords = new DogWords;
        }

        public function bark()
        {
            echo $this->dogname . "; Bark, bark, bark...";
        }
    }

    /**
    * Poodle
    */
    class Poodle extends Dog
    {

    }

    $Amy = new Poodle('DogConstructor');
    echo $Amy->dogwords->words[1];      // Fatal Error...
    echo $Amy->bark();      // DogConstructor; Bark, bark, bark...
 ?>
3
  • 1
    You should not have Dog extend DogWords. That's just silly. Commented Oct 21, 2013 at 17:50
  • Sure, Dog should inherit from Animals, Mammals, or HomePets. Because it should inherit from a class that is more abstract than dog itself. - For further readers... Commented Oct 21, 2013 at 19:23
  • Yes, that is correct. The problem is that you have an object inheriting from <name>Words. Having an abstract class which has a words property is one thing, having a dog be a subclass of bark is just wrong. Commented Oct 21, 2013 at 20:56

4 Answers 4

2

Protected members cannot be used outside the scope of the subclass , if you want to access it outside it should be declared as public .

Please study from here docs

Sign up to request clarification or add additional context in comments.

3 Comments

Ohh you right. I've seen in some frameworks that they use B extends A and then they use again parent::__construct(). But why would you do that exactly?
tats different that's for calling the parent constructor from derived class
But why would you call the constructor of the parent class when extending that class at the same time? When you make instance of B even when parent::__construct() will not be specified in B, PHP will still call the constructor of A because that's the nearest one. And B will still have all the properties and methods 'A' has.
1

If you pretend to keep the variable to protected, why just don't do the following:

class DogWords
{
    protected $words = array('Wrr', 'Grr', 'Weeee', 'Houou');
}

/**
* Dog Class
*/
class Dog extends DogWords
{
    protected $dogname;
    protected $dogwords;

    public function __construct($dogname)
    {
        $this->dogname = $dogname;
        $this->dogwords = new DogWords;
    }

    public function bark()
    {
        return $this->dogname . "; Bark, bark, bark...";
    }
}

/**
* Poodle
*/
class Poodle extends Dog
{
     public function getDogWords()
     {
         return $this->dogwords->words;
     }
}

$Amy = new Poodle('DogConstructor');
$words = $Amy->getDogWords();
var_dump($words[1]);
var_dump($Amy->bark());

Example: http://codepad.org/GsmmHxev

Comments

0

The error comes from this fact: "Members declared protected can be accessed only within the class itself and by inherited and parent classes".

http://www.php.net/manual/en/language.oop5.visibility.php

In order to access them, you need to make them both public:

class DogWords
{
    public $words = array('Wrr', 'Grr', 'Weeee', 'Houou');
}

/**
 * Dog Class
 */
class Dog extends DogWords
{
    protected $dogname = NULL;
    public $dogwords = NULL;

//... rest of the code

$Amy = new Poodle('DogConstructor');
echo $Amy->dogwords->words[1];      // Fatal Error...
echo $Amy->bark();      // DogConstructor; Bark, bark, bark...
?>

Now this prints: "GrrDogConstructor; Bark, bark, bark..."

Comments

0

According to the rule of OOP, a protected member can only be accessed from inside the base class and the derived class(immediate and non-immediate) but not from anywhere else.

But here you are trying to access the protected member of class dog i.e. dogwords in echo $Amy->dogwords->words[1] using the subclass poodle's object i.e. Amy. Hence according to the principle you cannot access it this way.

Perfectly legal...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.