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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值