1

I have following parent and child class.

class Parent_class {

    protected static function method_one() {
        echo "I am in Parent_class in method_one";
    }

    protected function execute() {
        static::method_one();
    }

    public function start() {
        $this->execute();
    }

}

class Child_class extends Parent_class {

    protected static function method_one() {
        echo "I am in Child_class in method_one";
    }

}

$obj = new Child_class();
$obj->start();

Result - it is calling Child class method.

The result is as expected because of static late binding is supported in php5.3 with the already reserved keyword static.

But the issue is, I do not have write access to Parent class, hence I can not use static while calling methode_one and hence it is not performing late static binding.

Is there any way out using which I can access overriding method ?

Parent class is a defined library, and I can not modify it.

Way out is to modify the parent class or drop this thought completely, but can you suggest any other alternative ?

Pranav
  • 2,054
  • 4
  • 27
  • 34

1 Answers1

0

Why not implement execute or start in child class?

xelber
  • 4,197
  • 3
  • 25
  • 33
  • thanx for the comment, In that case, i need to copy code of execute method, and then change self to static whenever required, but is it good programming practice ? and what if in future, someone changes parent class. – Pranav Jan 11 '13 at 07:00
  • Agree.. so you need to see if it's worth – xelber Jan 11 '13 at 07:04
  • Or I can override those two methods as per your suggestion and then I will call parent's functions on first line in my function itself.. might work :) – Pranav Jan 14 '13 at 04:11