class?aaaa{
private?static?$instance?=?null;
public?function?__construct(){
self::$instance?=?this;
}
public?static?function?getInstance(){
return?self::$instance
调用aaaa:getInstance()就是返回aaaa实例
作.在某些情况下,我们希望确定某个类是否特定的类型,或者是否实现了特定的接口.instanceof操作符非常适合完成这个任务.
instanceof操作符检查三件事情:实例是否某个特定的类型,实例是否从某个特定的类型继承,实例或者他的任何祖先类是否实现了特定的接口.例如,
假设希望了解名为manager的对象是否为类Employee的实例:
$manager?=?new?Employee();?
...
if?($manager?instanceof?Employee)?
echo?"Yes";
有两点值得注意.首先,类名没有任何定界符(引号).使用定界符将导致语法错误.其次,如果比较失败,脚本将退出执行.instanceof关键字在同时
处理多个对象时特别有用.例如,你可能要重复地调用某个函数,但希望根据对象类型调整函数的行为.可以使用case语句和instanceof关键字来实
现这个目标.
class?test{}?
class?testChilern?Extends?test{}?
$m?=?new?test();?
$i?=?($m?instanceof?test);?
if($i)
echo?'$m是类test的实例!br?/';?//?get?this?value?
switch?($a?instanceof?test){?
case?true?:?
echo?'YESbr?/';?
break;?
case?false?:?
echo?'Nobr?/';?//get?this?value?
}?
$d=new?testChilern();?
if($d?instanceof?test)echo?'$d是类test的子类!br?/';?//?get?this?value
php中 instanceof有什么作用
第一种用法:
php
$obj?=?new?A();
if?($obj?instanceof?A)?{
echo?'A';
第二种用法:
interface?ExampleInterface
{
public?function?interfaceMethod();
class?ExampleClass?implements?ExampleInterface
public?function?interfaceMethod()
return?'Hello?World!';
$exampleInstance?=?new?ExampleClass();
if($exampleInstance?instanceof?ExampleInterface){
echo?'Yes,?it?is';
}else{
echo?'No,?it?is?not';
输出结果:Yes, it is
另外,需注意 instanceof 与 is_subclass_of() 的区别,请看代码:
lass?Foo?{?public?$foobar?=?'Foo';
public?function?test()?{
echo?$this-foobar?.?"\n";
class?Bar?extends?Foo?{
public?$foobar?=?'Bar';
$a?=?new?Foo();
$b?=?new?Bar();
echo?"use?of?test()?method\n";
$a-test();
$b-test();
echo?"instanceof?Foo\n";
var_dump($a?instanceof?Foo);?//?TRUE
var_dump($b?instanceof?Foo);?//?TRUE
echo?"instanceof?Bar\n";
var_dump($a?instanceof?Bar);?//?FALSE
var_dump($b?instanceof?Bar);?//?TRUE
echo?"subclass?of?Foo\n";
var_dump(is_subclass_of($a,?'Foo'));?//?FALSE
var_dump(is_subclass_of($b,?'Foo'));?//?TRUE
echo?"subclass?of?Bar\n";
var_dump(is_subclass_of($a,?'Bar'));?//?FALSE
var_dump(is_subclass_of($b,?'Bar'));?//?FALSE
输出结果:Yes,?it?is
class?Foo?{
public?$foobar?=?'Foo';
use?of?test()?method
Foo
Bar
instanceof?Foo
bool(true)
instanceof?Bar
bool(false)
subclass?of?Foo
subclass?of?Bar
cache_factory?(这个是类名)::$cache_factory?(这个是类内的私有静态属性,该属性的值是cache_factory类的实例)-cache_config?=?$cache_config;?其他不需要解释了吧
不过这个代码很烂,不建议把这个当作参考资料.帮你修改了一下,看这个吧
final class cache_factory {
private static $instance;
protected $cache_config = array();
protected $cache_list = array();
protected function __construct(array $configs) {
$this-cache_config = $configs;
public static function get_instance(array $cache_config = null) {
if (! self::$instance instanceof cache_factory) {
if (is_null($cache_config)) {
die('无效的初始化设置');
self::$instache = new cache_factory($cache_configs);
return self::$instance;
case-1:给类中的静态变量赋值:
class?PHPJungle{
private?static?$__instance?=?null;#?类中的私有成员:静态变量
if(self::$__instance?instanceof?self)
return?self::$__instance;?#?给静态变量赋值
return?new?self();
public?function?anyMethod(){
echo?'Hello?world!','hr';
return;
$PJ?=?new?PHPJungle();
$PJ-getInstance()-anyMethod();
function?hello(){
static?$total?=?0;
echo?$total,'hr';
$total◆◆;?#?给函数中的局部静态变量赋值
hello();#?0
hello();#?1
单例模式
:使得类的一个对象成为系统中的唯一实例.
PHP中使用单例模式最常见的就是数据库操作了.避免在系统中有多个连接数据库的操作,浪费系统资源的现象,就可以使用单例模式.每次对数据库操作都使用一个实例.
简单示例
class
AClass
//
用来存储自己实例
public
static
$instance;
私有化构造函数,防止外界实例化对象
private
function
__construct()
{}
私有化克隆函数,防止外界克隆对象
__clone()
静态方法,单例访问统一入口
getInstance()
if
(!(self::$instance
instanceof
self)){
self::$instance
=
new
self();
return
self::$instance;
test
test()
"done";
BClass
extends
AClass{
获取实例
$aclass
AClass::getInstance();
$bclass
BClass::getInstance();
调用方法
echo
$aclass-test();
对一些比较大型的应用来说,可能连接多个数据库,那么不同的数据库公用一个对象可能会产生问题,比如连接句柄的分配等,我们可以通过给$instance变成数组,通过不同的参数来控制
DB
$instance
array();
$conn;
__construct($host,
$username,
$password,
$dbname,
$port)
$this-conn
mysqli($host,
$port);
getInstance($host,
$key
$host.":".$port;
(!(self::$instance[$key]
self::$instance[$key]
self($host,
$port);#实例化
self::$instance[$key];
//query
query($ql)
$this-conn-query($sql);
//释放资源
__destruct(){
$this-conn-close();
以上就是土嘎嘎小编为大家整理的php类instance的简单介绍php类instance的简单介绍相关咨询咨询主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!