0

Line: return 'Static hello ' . self::static_user . '...<br />'; Produces: Fatal error: Undefined class constant 'static_user' in ....

Note: Logic might be silly but I'm just playing wit it at the moment. Question is how can I access to static variable of abstract class in child class? I can access to public one $public_user with $this->public_user in child class.

abstract class UserAbstract
{
protected $public_user = null;
    static protected $static_user = null;

    public function __construct($name)
    {
    $this->public_user = $name;
        self::$static_user = $name;
    }

    abstract static function static_hello();
}

class User extends UserAbstract
{
    static function static_hello()
    {
        return 'Static hello ' . self::static_user . '...<br />';
    }
}

$obj_user = new User('Voodoo');
echo $obj_user->static_hello();

1 Answer 1

2

Use this:

return 'Static hello ' . parent::$static_user . '...<br />';
Sign up to request clarification or add additional context in comments.

4 Comments

I can't believe myself. How did I miss that..... Always something little. I actually know I have to use $ mark but sometimes happens, what can I say.. Thanks
I'm trying to use User::static_hello(); but doesn't print anything. Not even an error.
@MadMax : why will it print anything when this function only returns the value??
I think I should go and sleep now ... Missed echo this time ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.