class A
{
private static array $instances = [];
public readonly int $value;
public function __construct(?int $value = null)
{
if ($value !== null) {
$this->value = $value;
static::$instances[] = $this;
}
}
public function summ(): int
{
return array_reduce(
static::$instances,
fn($acc, $cur) => $acc + $cur->value,
0
);
}
}
new A(2);
new A(3);
$summ = (new A())->summ();
print $summ; // 5
trait Harvestable {
function harvest() { }
}
trait Waterable {
function water() { }
}
trait Repottable {
function repot() { }
};
class Plant { }
class Vegetable extends Plant {
use Waterable;
use Harvestable;
}
class Fruit extends Plant {
use Waterable;
use Harvestable;
use Repottable;
}
class Succulent extends Plant {
use Repottable;
}
class Foo
{
public function firstfunc()
{
echo 'hello ';
return $this;
}
public function secondfunc()
{
echo 'world ';
return $this;
}
}
$obj = new Foo();
$obj->firstfunc()->secondfunc();
class Baz
{
public function bar() {
$this->foo();
}
public function foo() {
$backtrace = debug_backtrace();
$stack = array_map(
function($el) {
return "{$el['class']}{$el['type']}{$el['function']}";
},
$backtrace
);
var_dump($stack);
}
}
$t = new Baz;
$t->bar();
/* array(2) {
[0] => string(8) "Baz->foo"
[1] => string(8) "Baz->bar"
} */