Instance child class in abstract static method PHP?

PHPServer Side ProgrammingProgramming

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
raja
Published on 13-Oct-2020 12:09:47
Advertisements