I
I
Ivan Ivanov2019-07-02 00:16:38
PHP
Ivan Ivanov, 2019-07-02 00:16:38

Will a protected static property of a child class be visible in another child class?

Hello!
We continue to tinker with OOP: The protected modifier allows access to the class itself, classes that inherit it, and parent classes.

class A{
    // тут мы видим self::$a
}
class B extends A{
    protected static $a;
}
class C extends A{
    // тут мы не видим self::$a
}

That is, if we see self::$a from child B in parent A, should it be visible in child C (from parent A)?
Or does such visibility imply EXACTLY direct inheritance and the parent does not act as a transit between the children, so to speak?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman PHP, 2019-07-02
@Artsiom_Ryzhanki

will not. a-priory.

<?php
class A{
    // тут мы видим self::$a как????
    public static function Foo() {
        echo self::$a;
    }
}
class B extends A{
    protected static $a;
}
class C extends A{
}

$a = new A();$b=new B();$c = new C();

var_dump($b instanceof A, $c instanceof A, $b instanceof C, $c instanceof B);
A::Foo();

bool(true)
bool(true)
bool(false)
bool(false)
Fatal error : Uncaught Error: Access to undeclared static property: A::$a in [...][...]:7
Stack trace:
# 0 [...][...](19): A::Foo()
#1 {main}
thrown in [...][...] on line 7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question