oop - Don't filters violate the a class' local variables visibility rules?

admin2025-06-04  1

Pressume I have a class:

class TestClass
{
    protected $handle;
}

and in that class, I decide that my $handle will be set by a filter, in the constructor:

class TestClass
{
    private $handle
    public function __construct()
    {
        $this->handle = apply_filters( 'handle_filter', '' );
    }
}

Have I not just violated / nullified the private statement that should dictate that, under no circumstance $handle should change, no matter what inheritance is done. But, due to it being a filter, it can change from anywhere so integrity is not assured.

In other words, private ensures integrity throughout the lifecycle of a class, whether it inherits or it's inherited, protected would allow for extension only by classes that work with my class.

It's really a contradiction and the private statement is useless then or am I missing something?


The other end of the stick is "this variable can change in many ways up until it reaches my class, once it reaches my class, it cannot change anymore and it will remain so unless the parent class decides it wants to change it", so, this could be a non-issue.

If this is true, then filters are violators of visibility until they're not, kind of like when kids do what they want but once the parents arrived home, they're silent?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749040099a315848.html

最新回复(0)