PHP 8.5.0 Alpha 4 available for testing

Voting

: three plus zero?
(Example: nine)

The Note You're Voting On

fabio at naoimporta dot com
9 years ago
It's possible to know how many clones have been created of a object. I'm think that is correct:

<?php

class Classe {

public static
$howManyClones = 0;

public function
__clone() {
++static::
$howManyClones;
}

public static function
howManyClones() {
return static::
$howManyClones;
}

public function
__destruct() {
--static::
$howManyClones;
}
}

$a = new Classe;

$b = clone $a;
$c = clone $b;
$d = clone $c;

echo
'Clones:' . Classe::howManyClones() . PHP_EOL;

unset(
$d);

echo
'Clones:' . Classe::howManyClones() . PHP_EOL;

<< Back to user notes page

To Top