3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class A{ static protected $shared = array(); static function create(){ if (isset(static::$shared['mailer'])){ return static::$shared['mailer']; } $mailer = new static(); //this is the correct way return static::$shared['mailer'] = $mailer; } } class B extends A{ } $obj=B::create(); $obj1=B::create(); var_dump($obj); var_dump($obj1);

preferences:
42.75 ms | 402 KiB | 5 Q