本文實例講述了php的instanceof和判斷閉包Closure。分享給大家供大家參考,具體如下:
類型運(yùn)算符
instanceof 用于確定一個 PHP 變量是否屬于某一類 class 的實例,在此之前用 is_a(),但是后來 is_a() 被廢棄
?php
class MyClass
{
}
class NotMyClass
{
}
$a = new MyClass;
var_dump($a instanceof MyClass);
var_dump($a instanceof NotMyClass);
?>
以上例程會輸出:
bool(true)
bool(false)
instanceof 也可用來確定一個變量是不是繼承自某一父類的子類的實例:
Example #2 對繼承類使用 instanceof
?php
class ParentClass
{
}
class MyClass extends ParentClass
{
}
$a = new MyClass;
var_dump($a instanceof MyClass);
var_dump($a instanceof ParentClass);
?>
以上例程會輸出:
bool(true)
bool(true)
Closure 類
用于代表 匿名函數(shù) 的類.
匿名函數(shù)(在 PHP 5.3 中被引入)會產(chǎn)生這個類型的對象。在過去,這個類被認(rèn)為是一個實現(xiàn)細(xì)節(jié),但現(xiàn)在可以依賴它做一些事情。自 PHP 5.4 起,這個類帶有一些方法,允許在匿名函數(shù)創(chuàng)建后對其進(jìn)行更多的控制。
除了此處列出的方法,還有一個 __invoke 方法。這是為了與其他實現(xiàn)了 __invoke()魔術(shù)方法 的對象保持一致性,但調(diào)用匿名函數(shù)的過程與它無關(guān)。
類摘要
Closure {
/* 方法 */
__construct ( void )
public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )
public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] )
}
Table of Contents
- Closure::__construct — 用于禁止實例化的構(gòu)造函數(shù)
- Closure::bind — 復(fù)制一個閉包,綁定指定的$this對象和類作用域。
- Closure::bindTo — 復(fù)制當(dāng)前閉包對象,綁定指定的$this對象和類作用域。
判斷是不是閉包
if ( $this->{$method} instanceof Closure ) {
return call_user_func_array($this->{$method},$args);
} else {
throw new Exception("Invalid Function");
}
//一切都要自行測試
if ( $class instanceof Closure ) {
} else {
}
參考:
http://php.net/manual/zh/language.operators.type.php
http://php.net/manual/zh/class.closure.php
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- 詳解PHP中instanceof關(guān)鍵字及instanceof關(guān)鍵字有什么作用
- php中instanceof 與 is_a()區(qū)別分析
- PHP 面向?qū)ο蟪绦蛟O(shè)計(oop)學(xué)習(xí)筆記(一) - 抽象類、對象接口、instanceof 和契約式編程
- php的閉包(Closure)匿名函數(shù)初探
- php的閉包(Closure)匿名函數(shù)詳解
- PHP閉包(Closure)使用詳解
- 淺析PHP中的閉包和匿名函數(shù)
- PHP中的閉包(匿名函數(shù))淺析
- PHP閉包實例解析
- PHP 閉包詳解及實例代碼