views:

158

answers:

2

I have a static function in a class that needs to be called from several child classes. I need a constant from the calling child class to be available in that function. To have these constants available in other places, the child classes have a function that returns the value of that constant (php 5.2.9).

However, when in the parent class, I can´t seem to access that constant, not with the function nor directly. Is that even possible in php 5.2.9 or do I need to pass it as an argument?

This is a simple version of the code:

abstract class ParentClass {
    static function DoSomething() {
        $not_working = self::show_const();
        $not_working_either = self::SOME_CONST;

        return 'working';
    }
}

class ChildClass extends ParentClass {
    const SOME_CONST = 'some string';

    function show_const() {
        return self::SOME_CONST;
    }
}

$result = ChildClass::DoSomething();

Edit: the error generated is:

  • Call to undefined method ParentClass::show_const() (for function)
  • Undefined class constant 'SOME_CONST' (using self::SOME_CONST)
+1  A: 

You need to make ChildClass extend parent class::

class ChildClass extends ParentClass {

Edit:

You're trying to refer to a constant and method in the child class from the parent class which doesn't know the child class' constant exists. It's a scope issue. The child can refer to the parent's methods and constants but not vice versa.

John Conde
That´s an error on my side in the example, I´ve corrected the question.
jeroen
+1 Succinctly expressed.
middaparka
Obviously you are right, as it is not working, but I am actually calling the child class, not the parent class (although the function resides in the parent class..), so it would seem logical to me that it´s properties would be available. Thanks anyway!
jeroen
+5  A: 

Unfortunately, what you're trying to do isn't going to work pre 5.3. The problem here is early static binding versus late static binding. The self keyword binds early, so it only looks in the class where it is used to resolve symbols. The magic constant __CLASS__ or the function get_class() won't work either, these do early static binding as well. For this reason PHP 5.3 extended the static keyword to mean late binding when used as static::some_method().

So in 5.3 this would work:

abstract class ParentClass {
  public static function DoSomething() {
    return static::show_const();
    // also, you could just do
    //return static::SOME_CONST;
  }
}

class ChildClass extends ParentClass {
  const SOME_CONST = 'some string';
  public static function show_const() {
    return self::SOME_CONST;
  }
}

$result = ChildClass::DoSomething();
fresch
Thanks, I was afraid of that. By the way, the only reason I´m using a function to return the constant, is that it seems the only way to have it available on objects in php < 5.3 ...
jeroen
+1 - The static::methodName() syntax is news to me. :-)
middaparka
+1 The lack of LSB can really make developing OO code in PHP (pre 5.3) a pain.
nategood