<?php
class Foo {
protected $b = null;
protected static $instance = null;
public static function get()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
public function __construct()
{
echo __METHOD__ . PHP_EOL;
$this->b = Bar::get();
}
public function __destruct()
{
echo __METHOD__ . PHP_EOL;
$this->b->func();
}
}
class Bar {
public $destroyed = false;
protected static $instance = null;
public static function get()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
public function __construct()
{
echo __METHOD__ . PHP_EOL;
}
public function __destruct()
{
echo __METHOD__ . PHP_EOL;
$this->destroyed = true;
}
public function func()
{
echo __METHOD__ . PHP_EOL;
}
}
Bar::get();
Foo::get();
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- Bar::__construct
Foo::__construct
Bar::__destruct
Foo::__destruct
Bar::func
preferences:
283 ms | 404 KiB | 326 Q