Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Variable Handling Functions : is_object

is_object

Finds whether a variable is an object (PHP 4, PHP 5)
bool is_object ( mixed var )


Related Examples ( Source code ) » is_object





Code Examples / Notes » is_object

corychristison at- lavacube .dot com

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 />' . "\n";
}
else
{
// do something different for procedural method
echo 'this is a procedure call <br />' . "\n";
}
}
}
$inst = new mrClass();
$inst->test();
mrClass::test();
?>
This would output:
this is an instance call <br />
this is a procedure call <br />
:-) Happy coding!


peter

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


lbjay can be emailed

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.


victor

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


xixulon

(addendum to function below: for clarity, don't forget to return false;)

Change Language


Follow Navioo On Twitter
debug_zval_dump
doubleval
empty
floatval
get_defined_vars
get_resource_type
gettype
import_request_variables
intval
is_array
is_binary
is_bool
is_buffer
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_object
is_real
is_resource
is_scalar
is_string
is_unicode
isset
print_r
serialize
settype
strval
unserialize
unset
var_dump
var_export
eXTReMe Tracker