最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解PHP中instanceof關(guān)鍵字及instanceof關(guān)鍵字有什么作用

 更新時(shí)間:2015年11月05日 14:19:47   投稿:mrr  
這篇文章主要介紹了PHP中instanceof關(guān)鍵字詳解及instanceof關(guān)鍵字有什么作用的相關(guān)資料,需要的朋友可以參考下

PHP5的另一個(gè)新成員是instdnceof關(guān)鍵字。使用這個(gè)關(guān)鍵字可以確定一個(gè)對象是類的實(shí)例、類的子類,還是實(shí)現(xiàn)了某個(gè)特定接口,并進(jìn)行相應(yīng)的操作。在某些情況下,我們希望確定某個(gè)類是否特定的類型,或者是否實(shí)現(xiàn)了特定的接口。instanceof操作符非常適合完成這個(gè)任務(wù)。instanceof操作符檢查三件事情:實(shí)例是否某個(gè)特定的類型,實(shí)例是否從某個(gè)特定的類型繼承,實(shí)例或者他的任何祖先類是否實(shí)現(xiàn)了特定的接口。例如,假設(shè)希望了解名為manager的對象是否為類Employee的實(shí)例:

$manager = new Employee();
…
if ($manager instanceof Employee)
  echo "Yes";

有兩點(diǎn)值得注意。首先,類名沒有任何定界符(引號)。使用定界符將導(dǎo)致語法錯(cuò)誤。其次,如果比較失敗,腳本將退出執(zhí)行。instanceof關(guān)鍵字在同時(shí)處理多個(gè)對象時(shí)特別有用。例如,你可能要重復(fù)地調(diào)用某個(gè)函數(shù),但希望根據(jù)對象類型調(diào)整函數(shù)的行為??梢允褂胏ase語句和instanceof關(guān)鍵字來實(shí)現(xiàn)這個(gè)目標(biāo)。

class test{}
class test{}
class testChilern Extends test{}
$a = new test();
$m = new test();
$i = ($m instanceof test);
if($i)
  echo '$m是類test的實(shí)例!<br />'; // get this value
switch ($a instanceof test){
  case true :
    echo 'YES<br />';
    break;
  case false :
    echo 'No<br />'; //get this value
    break;
}
$d=new testChilern();
if($d instanceof test)echo '$d是類test的子類!<br />'; // get this value

php中 instanceof有什么作用

作用:(1)判斷一個(gè)對象是否是某個(gè)類的實(shí)例,(2)判斷一個(gè)對象是否實(shí)現(xiàn)了某個(gè)接口。

第一種用法:

<?php
$obj = new A();
if ($obj instanceof A) {
  echo 'A';
}

第二種用法:

<?php
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';
} 
?>

輸出結(jié)果:Yes, it is

另外,需注意 instanceof 與 is_subclass_of() 的區(qū)別,請看代碼:

<?php
class 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
?>

 輸出結(jié)果(PHP 5.4.4):

 use of test() method
 Foo
 Bar
 instanceof Foo
 bool(true)
 bool(true)
 instanceof Bar
 bool(false)
 bool(true)
 subclass of Foo
 bool(false)
 bool(true)
 subclass of Bar
 bool(false)

以上內(nèi)容是本文給大家介紹的PHP中instanceof關(guān)鍵字及instanceof關(guān)鍵字有什么作用的全部內(nèi)容,希望大家喜歡。

相關(guān)文章

最新評論

玉山县| 资溪县| 宜昌市| 炎陵县| 凤庆县| 玛多县| 深圳市| 宽甸| 瓦房店市| 南城县| 原阳县| 资中县| 绥宁县| 吴旗县| 澄迈县| 蓬莱市| 古交市| 江源县| 凌海市| 拉孜县| 兖州市| 巴彦县| 天峻县| 高安市| 涿州市| 新津县| 沁水县| 防城港市| 逊克县| 九寨沟县| 铜鼓县| 黔西县| 绥中县| 兴文县| 邯郸县| 肥城市| 水城县| 隆林| 南城县| 罗甸县| 阿拉善盟|