欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

PHP is_object 检测变量是否是一个对象

shiping1 的头像

is_object

(PHP 4, PHP 5)

is_object — 检测变量是否是一个对象

描述

bool is_object ( mixed $var )

如果 var 是一个 object 则返回 TRUE,否则返回 FALSE

参见 is_bool()is_int()is_integer()is_float()is_string() 和 is_array()


 

PHP is_object note #1

Unserializes data as returned by the standard PHP serialize() function. If the unserialized object is not an array, it will be converted to one, particularily useful if it returns a __PHP_Incomplete_Class. 

<?php 
/** 
 * 
 * @param string $data Serialized data 
 * 
 * @return array    Unserialized array 
 */ 

function unserialize2array($data) { 
    
$obj unserialize($data); 
    if(
is_array($obj)) return $obj
    
$arr = array(); 
    foreach(
$obj as $k=>$v) { 
        
$arr[$k] = $v
    } 
    unset(
$arr['__PHP_Incomplete_Class_Name']); 
    return 
$arr

?>

PHP is_object note #2

Note: is_object(null) returns false

This should actually be part of the input/output specification at the top of this page.

PHP is_object note #3

Just discovered:
is_a  (  object $object  ,  string $class_name  )
Which checks if the object is of this class or has this class as one of its parents

Which seems to do what a lot here are trying to replicate

PHP is_object note #4

Use instanceof() to check for a specific type.

PHP is_object note #5

Cleaning it up even more:

<?php
function is_obj(&$object$className null$caseSensitive true) {
    return 
is_object($object) && (!is_string($className) ||preg_match('/^'.$className.'$/D'.($caseSensitive '' 'i'),get_class($object)));
}

?>

PHP is_object note #6

cleaned up peter's code... use only one return statement

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

PHP is_object note #7

Optimizing the is_obj() from corychristison, and with the "return false" suggested by xixulon.

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

PHP is_object note #8

Thank you victor AT fourstones DOT net.

I have written a function to do what victor has suggested, with the ease of use of is_object. It can be used to replace is_object(), but has an extra field [$check], to compare to a certain name. If $check is left empty, it will just check if &$object is an object.

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


?>

This could probably be cleaned up, but it's spaced out to be easy to read.

PHP is_object note #9

er, I don't think that's right, especially if calling from another object instance:

<?

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 ");
            else
                print("this is ok " );
        }
        else
        {
            print("static call ");
        }
    }
}

test_this();
?>

yields:
---------- run-php ----------

oops
this is ok
static call

PHP is_object note #10

You can use is_object($this) to detect if the function is being called via instance or procedure.

Example:

<?php

class mrClass {

    function 
test( )
    {
        if( 
is_object($this) )
        {
         
// do something for instance method
            
echo 'this is an instance call <br />' " ";
        }
        else
        {
         
// do something different for procedural method
            
echo 'this is a procedure call <br />' " ";
        }
    }

}


$inst = new mrClass();
$inst->test();

mrClass::test();

?>

This would output:
this is an instance call <br />
this is a procedure call <br />

:-) Happy coding!

PHP is_object note #11

I'm not even sure how to articulate this, so I'm going to just include test code. Maybe someone else will someday wonder the same thing.

<?
    error_reporting(E_ALL);
    class testParent
    {
        var $child;

        function testParent()
        {
            $this->child = new testChild();
        }
    }

    class testChild
    {
        function testChild()
        {
        }
    }

    $parent = new testParent();
    $parent2 = 'foobar';

    print join(',', Array(
        is_object($parent) ? 'yes' : 'no',
        is_object($parent->child) ? 'yes' : 'no',
        is_object($parent2) ? 'yes' : 'no',
        is_object($parent2->child) ? 'yes' : 'no'
    ));

?>

This prints "yes,yes,no,no". Basically this shows that you can use is_object to test if the child object is an object without worrying about an error if the parent object isn't an object either.

来自 http://www.php100.com/cover/php/1069.html

普通分类: