5

Can someone help me understand variable/function inheritance in PHP classes.

My parent class has a function which is used by all the child classes. However each child classes needs to use it's own variable in this function. I want to call the function in the child classes statically. In the example below, the 'world' is displayed, as opposed to the values in the child classes.

Can anyone explain how I can get the function to echo the values in the child classes. Should I be using interfaces? Is this something to do with late static binding (which is unavailable to me due to using a pre 5.3.0 version of PHP)?

class myParent
{
    static $myVar = 'world';
    static function hello()
    {
        echo self::$myVar;  
    }
}

class myFirstChild extends myParent
{
    static $myVar = 'earth';
}

class mySecondChild extends myParent
{
    static $myVar = 'planet';
}

myFirstChild::hello();
mySecondChild::hello();
1
  • An interface just defines the functions that need to be defined, it wouldn't work for something like this. Late static binding is what you want, but as you said, you can't use it. What exactly do you need this behavior for? There might be a better way to implement it. Commented Aug 6, 2009 at 1:01

4 Answers 4

2

Yeah, you can't do that. The declarations of static $myVar do not interact with each other in any way, precisely because they are static, and yeah, if you had 5.3.0 you could get around it, but you don't so you can't.

My advice is to just use a non-static variable and method.

Sign up to request clarification or add additional context in comments.

1 Comment

I've decided to create an instance of 'self' inside the children's static functions, then store the variables as non-static.
1

You could do it like this:

class myParent
{
    var $myVar = "world";
    function hello()
    {
        echo $this->myVar."\n";      
    }
}

class myFirstChild extends myParent
{
    var $myVar = "earth";
}

class mySecondChild extends myParent
{
    var $myVar = "planet";
}

$first = new myFirstChild();
$first->hello();

$second = new mySecondChild();
$second->hello();

This code prints

earth
planet

1 Comment

you are inheriting constants here, not variables.
0

IF you were using PHP 5.3, this echo statement would work:

echo static::$myVar;

But since that is unavailable to you, your only (nice) option is to make the hello() function not static.

Comments

0

I want to call the function in the child classes statically.

This will really drive you into troubles that'll get you nuts before the end of the day ^^ (It already has, maybe ^^ )

I would defintly recommend using as few "static" properties/methods as possible, especially if you are trying to work with inheritance, at least with PHP < 5.3

And as PHP 5.3 is quite new, it probably won't be available on your hosting service before a couple of months...

Comments

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.