Title: PHP OO: Calling methods on abstract classes?
Tags: php;oop
Question: Please could someone help with the following?

We have the following classes:


abstract class Element{}
abstract class Container extends
Element{}
abstract class Field extends
Container{}


The 'Element' base class has the following properties and methods:

```//Element class
private $errors = array();

public function __construct()
{
}

public function setError($error)
{
$this-&gt;errors[] = $error;
}

public function getErrors()
{
return "<li&gt;".implode("</li&gt;\n",$this-&gt;errors);
}
```

The 'Container' just groups the elements (objects).

The 'Field' class calls the 'setError' method of the base class and passes a value like this:

```//Field class
$this-&gt;setError("foo");
```

For some reason the 'errors' property in the base class doesn't get the value added to it and Im guess its something to do with how the object is instantiated because obviously the abstract classes are not instantiated by default.

The only instantiation of the field is in its inherited form which is:

```Text extends Field{}
$field = new Text(etc, etc)
```

How would you get about resolving this?

Comment: can you show the full code for element and field classes

Comment: "Favor object composition over class inheritance." (c) GoF

Comment: How is this question unclear for whoever felt the next to vote it down?

Here is the accepted answer: Works for me : http://codepad.viper-7.com/Ool7zn

```<?php

abstract class Element
{
    protected $errors = array();

    public function setError($error)
    {
        $this-&gt;errors[] = $error;
    }

    public function getErrors()
    {
        return "<li&gt;".implode("</li&gt;\n",$this-&gt;errors);
    }
}
abstract class Container extends Element{}
abstract class Field extends Container{}
class Text extends Field{}

$t = new Text;
$t-&gt;setError('foobar');

echo $t-&gt;getErrors();

?&gt;
```

Comment for this answer: Can you paste your code into your answer so that we don't lose the solution, please.
Comment for this answer: note that you didn't show how it is called + it really doesn't matter how it is called.
Comment for this answer: Thanks Teresko but the 'setError' method is not called like that.
Comment for this answer: I did show how its called. It's called within the 'field' class and ofcourse it matters how its called. You cant call it out of thin air but thanks anyway.
Comment for this answer: Theres obviously something else going on but Tereskos example is good because it shows this should work so going to put this as answered.

Here is another answer: You're going to have to set the $errors member variable to protected in class Element.

```//Element class
protected $errors = array();
```

Right now, when you call the inherited setError() function on the Text class instance, the Text class instance does not have its own $errors array, so PHP does you the 'favor' of creating one on the fly within the instance of the Text class.  However, this is a different $errors member variable from the one in the Elements base class.

Setting the $errors member variable to 'protected' allows the instance of the Text class to interact with the variable in the Element base class, so that PHP does not do you the 'favor' of creating a new $error member (belonging only to the Text class) on the fly.

Comment for this answer: You're not re-declaring $errors in any of the inheriting classes?
Comment for this answer: Also, what method in the Field class contains the call to  $this->setError("foo")?

Are you calling that same (inherited) method on the Text class?  I.e. $text->theMethod() (with theMethod being inherited from Field).
Comment for this answer: FYI, might be best to have the code of all the classes involved.
Comment for this answer: Thanks DWight, good answer but didn't work. Is it because the 'setError' method is called in 'Field' class but its actually 'Text' class thats gets instantiated. I know 'Text' is inheriting 'Field' but thought Id ask?
Comment for this answer: No not re-declaring $errors anywhere. In the 'field' class is a method named 'validate', this is an abstract method for near enough all inheriting classes. The 'Text' class only has a 'getfield' method. Sorry.
Comment for this answer: How is adding something that has no relevance to the question going to help. There are so many classes and functionality shared between them its only going to confuse matters but thanks anyway Galen.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:04/28/11', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<php><oop>', 'Title': 'PHP OO: Calling methods on abstract classes?', 'ViewCount': 5637, 'id': 5823134, 'user_AboutMe': None, 'user_AccountId': 747509.0, 'user_DisplayName': 'Steve Took', 'user_Reputation': 161.0, 'user_Views': 9.0, 'user_id': 727270.0}}