php中檢測變量是否是一個對象的is_object函數介紹及用法舉例

php中檢測變量是否是一個對象的is_object函數介紹及用法舉例


php函數is_object:檢測變量是否是一個對象

函數描述

bool is_object ( mixed $var )

如果 var 是一個 object 則返回 TRUE,否則返回 FALSE。

參見 is_bool()、is_int()、is_integer()、is_float()、is_string() 和 is_array()。

注意: is_object(null) 返回false.

is_object使用代碼舉例1:


<?php
function is_obj( &$object, $check=null, $strict=true )
 {
     if( $check == null && is_object($object) )
     {
         return true;
     }
     if( is_object($object) )
     {
         $object_name = get_class($object);
         if( $strict === true )
         {
             if( $object_name == $check )
             {
                 return true;
             }
         }
         else
         {
             if( strtolower($object_name) == strtolower($check) )
             {
                 return true;
             }
         }
     }
 }
?>


  is_object使用代碼舉例2:

<?
 function test_this()
 {
     $c2 = new C2();
     $c2->func();
     $c1 = new C1();
     $c1->func();
     C1::func();
 }
 class C2
 {
     function func()
     {
         C1::func();
     }
 }
 class C1
 {
     function func()
     {
         if( isset($this) )
         {
             if( strtolower(get_class($this)) != 'c1' )
                 print("oops\n");
             else
                 print("this is ok\n" );
         }
         else
         {
             print("static call\n");
         }
     }
 }
 test_this();
 ?>


運行輸出結果如下:

---------- run-php ----------


oops

this is ok

static call


發佈了0 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章