Please advise what's going on here?
We have the following classes working together:
- An abstract base class 'form element'
- An abstract 'container' class that extends the form element class and groups form elements
- A 'field' class that extends the container
In the 'field' class we call a method from the 'form element' class named 'setErrors($vars)' that sets a property named 'hasErrors'. $vars carries a boolean.
There is another method named 'hasErrors()' in the 'form element' class that we try to call in the 'field' class to test the result of the boolean previously set however it returns false.
The hasErrors property that is initially declared as false is not being overwritten?
Form Element Class(Base)
private $hasErrors = false;
public function setErrors($flag)
{
...
$this->hasErrors = $flag;
...
}
public function hasErrors()
{
return $this->hasErrors;
}
Field Class
public function validate($value)
{
...
$this->setErrors($foundErrors);//$foundErrors only local to this method
...
}
public function getField()
{
...
if($this->hasErrors())
{
//do something...
}
...
}
Why is the property 'hasErrors' not being overwritten? Would this have something to with the scope of inheritance between the different classes?
Not sure how this works but thanks in advance.
validateyou are setting the value using the public setter methodsetErrors. That's perfectly legal. But where is theif($this->hasErrors())located? If I put it inside thevalidatemethod (you have it outside somewhere), it works properly, as it should. Whoops, nevermind. I overlooked that you have a container class, too (class field extends container extends formelement).