php检查一个变量是否定义,PHP 检测变量是否是一个对象

用户评论:

mark at not4you dot com (2011-09-21 12:23:15)

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.

*

* @param string $data Serialized data

*

* @return array    Unserialized array

*/functionunserialize2array($data) {$obj=unserialize($data);

if(is_array($obj)) return$obj;$arr= array();

foreach($objas$k=>$v) {$arr[$k] =$v;

}

unset($arr['__PHP_Incomplete_Class_Name']);

return$arr;

}?>

peter dot nagel at portavita dot nl (2011-03-22 01:46:48)

Note: is_object(null) returns false

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

will (2010-03-20 14:36:22)

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

ldean at saleamp dot com (2009-09-01 07:16:07)

Use instanceof() to check for a specific type.

Senthryl (2009-03-17 06:03:03)

Cleaning it up even more:

returnis_object($object) && (!is_string($className) ||preg_match('/^'.$className.'$/D'.($caseSensitive?'':'i'),get_class($object)));

}?>

gregdangelo at gmail dot com (2008-04-14 12:03:43)

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;

}

peter at i-node dot com dot br (2006-05-20 01:03:44)

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;

}

}

corychristison[aT-]lavacube(.dot)com (2005-02-02 17:06:59)

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.

{

if($check==null&&is_object($object) )

{

returntrue;

}

if(is_object($object) )

{$object_name=get_class($object);

if($strict===true)

{

if($object_name==$check)

{

returntrue;

}

}

else

{

if(strtolower($object_name) ==strtolower($check) )

{

returntrue;

}

}

}

}?>

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

victor AT fourstones DOT net (2005-01-01 15:49:38)

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\n");

else

print("this is ok\n" );

}

else

{

print("static call\n");

}

}

}

test_this();

?>

yields:

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

oops

this is ok

static call

corychristison[aT-]lavacube(.dot)com (2005-01-01 07:47:56)

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

Example:

functiontest( )

{

if(is_object($this) )

{// do something for instance methodecho'this is an instance call 
'."\n";

}

else

{// do something different for procedural methodecho'this is a procedure call 
'."\n";

}

}

}$inst= newmrClass();$inst->test();mrClass::test();?>

This would output:

this is an instance call 

this is a procedure call 

:-) Happy coding!

lbjay can be emailed at reallywow dot com (2003-05-02 12:18:49)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值