本文整理汇总了PHP中Singleton::instance方法的典型用法代码示例。如果您正苦于以下问题:PHP Singleton::instance方法的具体用法?PHP Singleton::instance怎么用?PHP Singleton::instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Singleton
的用法示例。
在下文中一共展示了Singleton::instance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: singleton
public static function singleton($classname)
{
if (!isset(self::$instance)) {
self::$instance = new $classname();
}
return self::$instance;
}
示例2: test_find_by
function test_find_by()
{
setup_sqlite_test_db();
# PHP 5.3 needed to call magic methods statically, so
# work around by instanciating the class as singleton
$Test = Singleton::instance("Test");
# create two test entries to be found
Test::create(array("dummy" => "Test 1"), array("dummy" => "Test 2"));
# Test find_by single id
$dummy = $Test->find_by_id(1);
$this->assertEqual(strtolower(get_class($dummy)), "test");
$this->assertEqual($dummy->dummy, "Test 1");
# Test find_by id array
$dummies = $Test->find_by_id(array(1, 2), array("order" => "id"));
$this->assertEqual(count($dummies), 2);
$this->assertEqual($dummies[0]->dummy, "Test 1");
$this->assertEqual($dummies[1]->dummy, "Test 2");
# Test it works with strings, too
$dummy = $Test->find_by_dummy("Test 2");
$this->assertEqual($dummy->id, 2);
# Test their combination
$dummy = $Test->find_by_id_and_dummy(1, "Test 1");
$this->assertEqual($dummy->id, 1);
$this->assertEqual($dummies[0]->dummy, "Test 1");
# Test their array'ed combination
$dummies = $Test->find_by_id_and_dummy(array(1, 2), array("Test 1", "Test 2"), array("order" => "id"));
$this->assertEqual(count($dummies), 2);
$this->assertEqual($dummies[0]->dummy, "Test 1");
$this->assertEqual($dummies[1]->dummy, "Test 2");
}
示例3: test_sti_field_is_saved_and_used_correctly
function test_sti_field_is_saved_and_used_correctly()
{
setup_sqlite_test_db();
# Create a dummy test entries
$dummy = Test::create(array("dummy" => "TestDummy"));
$this->assertEqual($dummy->type, NULL);
$stest = new SuperTest(array("dummy" => "SuperDummy"));
$stest->save();
$this->assertEqual(strtolower($stest->type), "supertest");
$utest = new UltraTest(array("dummy" => "UltraDummy"));
$utest->save();
$this->assertEqual(strtolower($utest->type), "ultratest");
$atest = new AutoTest(array("dummy" => "AutoDummy"));
$atest->save();
$this->assertEqual(strtolower($atest->type), "autotest");
# PHP 5.3 needed to call magic methods statically, so
# work around by instanciating the class as singleton
$Test = Singleton::instance("Test");
# Load and check test entries
$dummy2 = $Test->find_by_dummy("TestDummy");
$this->assertEqual($dummy2->type, NULL);
$this->assertEqual(strtolower(get_class($dummy2)), "test");
$stest2 = $Test->find_by_dummy("SuperDummy");
$this->assertEqual(strtolower($stest2->type), "supertest");
$this->assertEqual(strtolower(get_class($stest2)), "supertest");
$utest2 = $Test->find_by_dummy("UltraDummy");
$this->assertEqual(strtolower($utest2->type), "ultratest");
$this->assertEqual(strtolower(get_class($utest2)), "ultratest");
$atest2 = $Test->find_by_dummy("AutoDummy");
$this->assertEqual(strtolower($atest2->type), "autotest");
$this->assertEqual(strtolower(get_class($atest2)), "autotest");
}
示例4: get_instance
public static function get_instance()
{
if (!isset(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
}
示例5: getInstance
public static function getInstance()
{
if (!self::$instance instanceof self) {
self::$instance = new Singleton();
}
return self::$instance;
}
示例6: get_instance
public static function get_instance()
{
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
示例7: getInstance
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
示例8: getInstance
public static function getInstance()
{
if (empty(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
示例9: getInstance
static function getInstance()
{
if (empty(self::$instance)) {
self::$instance = new Singleton();
}
return self::$instance;
}
示例10: getInstance
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new Singleton();
}
return self::$instance;
}
示例11: GetInstance
public static function GetInstance()
{
if (Singleton::$instance == null) {
Singleton::$instance = new Singleton();
}
return Singleton::$instance;
}
示例12: getInstance
public static function getInstance()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}
示例13: getInstance
public static function getInstance()
{
if (empty($instance)) {
self::$instance = new Singleton();
self::$instance->loadValues();
}
return self::$instance;
}
示例14: instance
static function instance()
{
if (!isset(self::$instance)) {
$c = get_called_class();
self::$instance = new $c();
}
return self::$instance;
}
示例15: getInstance
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new self();
echo 1;
return self::$instance;
}
}