Control the number of instances of the PHP Class! In order to ensure the efficient use of server resources, and a class, such as templates Ah, database what, in a page only need to instantiate once! That is, running only one instance in memory! To avoid repeated instantiation, then control the number of instances of the PHP class, is very necessary! The method is actually very simple: to give class an external interface, privatize (private) constructor, discard the method that can instantiate the class outside of the class using new! The following is the example I gave, I believe you will see it! (PHP5 above version!)
<?php
Class test{
Const NAME = ' Test ';
public static $havenew = false;
Public $name = ' I am limited to instantiating only once! ';
Private Function __construct () {
}
function __destruct () {
Self:: $havenew = false;
}
Public function Inter () {
if (self:: $havenew) {
echo ' class '. Self::name. ' has been instantiated! ';
return NULL;
}else{
Self:: $havenew = true;
return new self;
}
}
}
$class 1 = test::inter ();
Var_dump ($class 1);
echo ' <br/> ';
$class 2 = Test::inter ();
Var_dump ($class 2);
?>
Here are examples of objects that are instantiated through the Inter () method in test! Because the construct itself cannot be accessed directly, there is no way to instantiate it with new!