0

I've the following classes:

<?php
    Abstract Class A{
        public function foo(){
            self::bar();
        }

       Abstract public static function bar($arg);
    }

    Class B extends A{ 
        public static function bar(){
            echo "Class B";      
        }
    }

    Class C extends A{
        public static function bar(){
            echo "Class C";
        }
    }
?>

I need to that the method bar() for the instantiated class be called from foo():

<?php
  $obj1 = new B();
  $obj2 = new C();

  $obj1->foo(); // I expect to get 'Class B'
  $obj2->foo(); // I expect to get 'Class C'
?>

Thanks in advance.

hakre
  • 193,403
  • 52
  • 435
  • 836
Mahdi Mojiry
  • 21
  • 1
  • 4

1 Answers1

0

You use late static binding for this

Basically in the abstract class you call static::method(). This uses the method of the class that made the call.

Abstract Class A{
    public function foo(){
        // Late static binding
        static::bar();
    }

   abstract public static function bar();
}
Galen
  • 29,976
  • 9
  • 71
  • 89