本文整理汇总了PHP中MyClass::getInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP MyClass::getInstance方法的具体用法?PHP MyClass::getInstance怎么用?PHP MyClass::getInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MyClass
的用法示例。
在下文中一共展示了MyClass::getInstance方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: MyClass
<?php
// Assuming every method call below returns an instance of an object, how can the following be re-written in PHP 5?
$a = new MyClass();
$b = $a->getInstance();
$c = $b->doSomething();
/*
1) $c = ((MyClass)$a->getInstance())->doSomething();
2) This cannot be re-written in PHP 5
3) $c = $a->getInstance()->doSomething(); // PHP 5+
4) $c = (MyClass)$a->getInstance();
5) $c = (new MyClass())->getInstance()->doSomething(); // PHP 5.4+ http://php.net/manual/en/migration54.new-features.php
*/
示例2: __construct
<?php
include_once __DIR__ . "/../vendor/autoload.php";
use useful\traits\Singleton;
class MyClass
{
use Singleton;
public $hash;
function __construct()
{
$this->hash = md5(rand());
}
}
class MyClass2 extends MyClass
{
use Singleton;
// note: without defining, it will use existent instance of class MyClass
public function getById()
{
}
}
echo MyClass::getInstance()->hash . PHP_EOL;
echo MyClass2::getInstance()->hash . PHP_EOL;