diff --git a/language/oop5/patterns.xml b/language/oop5/patterns.xml index aa57a83502..db5faf061f 100644 --- a/language/oop5/patterns.xml +++ b/language/oop5/patterns.xml @@ -1,5 +1,5 @@ - + Patterns @@ -74,7 +74,7 @@ class Example // Hold an instance of the class private static $instance; - // A private constructor + // A private constructor; prevents direct creation of object private function __construct() { echo 'I am constructed'; @@ -96,6 +96,13 @@ class Example { echo 'Woof!'; } + + // Prevent users to clone the instance + public function __clone() + { + trigger_error('Clone is not allowed.', E_USER_ERROR); + } + } ?> @@ -115,6 +122,9 @@ $test = new Example; $test = Example::singleton(); $test->bark(); +// This will issue an E_USER_ERROR. +$test_clone = clone($test); + ?> ]]>