When I try to Override the class variable same way as override the class method in PHP. Like:
class DataMapper {
protected $_name = null;
public function printName() {
echo $this->_name;
}
}
class Model extends DataMapper {
protected $_name = 'Ana';
}
$test = new Model();
$test->printName();
It’s print ‘Ana’.
Why PHP can do such a thing like that ? It break the law of object oriented paradigm
It’s not. That’s how PHP is supposed to work. Have a look at PHP Classes and Objects Visibility.
Because Model extends DataMapper, it has access to its functions, variables and such but it can override them which is what happened. Although your function lives in the DataMapper class, it’s called from (and inherited by) the Model class in which the name is set to Ana.