以下为引用的内容:
<?php
// Get instance of DBConnection
$db =& DBConnection::getInstance();
// Set user property on object
$db->user = 'sa';
// Set second variable (which points to the same instance)
$second =& DBConnection::getInstance();
// Should print 'sa'
echo $second->user;
Class DBConnection {
var $user;
function &getInstance() {
static $me;
if (is_object($me) == true) {
return $me;
}
$me = new DBConnection;
return $me;
}
function connect() {
// TODO
}
function query() {
// TODO
}
}
?>