Instance child class in abstract static method PHP?



For this, use $anyObjectName=new static() along with self.

Example

The PHP code is as follows

 Live Demo

<!DOCTYPE html> <html> <body> <?php abstract class Base{    protected static $fullName = '';    abstract protected function customFunction();    public static function get_obj($param1 = null, $param2 = null){       $obj = new static();       $obj->customFunction();    }    public static function getFullName(){       return static::$fullName;    } } class Child extends Base {    protected static $fullName = 'John Doe';    protected function customFunction(){       echo self::getFullName() . "<br>";       echo  $this;    } } Child::get_obj(); ?> </body> </html>

Output

This will produce the following output

John Doe
Updated on: 2020-10-13T08:40:06+05:30

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements