There is one more option. When you close your __clone () private account, the following will be called when cloning:
PHP7.x FATAL ERROR Uncaught Error: calling private A :: __ clone () from context
PHP5.x FATAL ERROR Call private A :: __ clone () from context
So, if you want your code to behave exactly the same among different versions of PHP, you can do the following:
<?php
class A {
public function __clone() {
throw new Exception('Not allowed to be cloned');
}
}
$a = new A;
try {
$b = clone $a;
} catch (Exception $e) {
echo $e->getMessage();
}
Or more "fancy":
trait NotClonable {
public function __clone() {
throw new Exception('Not allowed to be cloned');
}
}
class A {
use NotClonable;
}
source
share