SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 88 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"

If you execute the following script, you will be
surprised to see the result.
$a = new MYSQLManager();
$b = new MYSQLManager();
$c = new MYSQLManager();
$d = new MYSQLManager();
$e = new MYSQLManager();
?>
The output is:
New Instance
Old Instance
Old Instance
Old Instance
Old Instance
Chapter 4
[ 81 ]
Strange, isn't it? The MySQLManager class creates only a single instance at the very
first call, after that it is using the same old object instead of creating a new object all
the time. Let us see how we achieve it.
private static $instance;
Our class has a static variable named $instance. At the constructor we check if the
static variable actually contains anything. If it is empty, we instantiate the object
itself and set the instance in this static variable. As it is static, it will remain available
throughout the execution of this script.
Let us get back to the constructor. At the second call, we just check if the $instance
variable contains anything. We find that the $instance variable is actually
containing an instance of this object, and it is still preserved because it is a static
variable. So in the second call, we actually return the instance of this object, which
was created by the previous call.


Pages:
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6