downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

is_real> <is_numeric
Last updated: Fri, 20 Nov 2009

view this page in

is_object

(PHP 4, PHP 5)

is_objectDetermină dacă o variabilă este un obiect

Descrierea

bool is_object ( mixed $var )

Determină dacă variabila dată este un obiect.

Parametri

var

Variabila ce este evaluată.

Valorile întroarse

Întoarce TRUE dacă var este un object, FALSE în caz contrar.

Exemple

Example #1 Exemplu is_object()

<?php
// Declară o funcţie simplă ce întoarce un tablou
// din obiectul nostru
function get_students($obj)
{
    if(!
is_object($obj))
    {
        return(
false);
    }

    return(
$obj->students);
}

// Declară o nouă instanţă a clasei şi completează
// unele valori
$obj = new stdClass;
$obj->students = Array('Kalle''Ross''Felipe');

var_dump(get_students(NULL));
var_dump(get_students($obj));
?>

Note

Notă: Această funcţie va întoarce FALSE dacă este utilizată cu un obiect neserializat, unde definiţia clasei nu este prezentă (cu toate că gettype() întoarce object).

Vedeţi de asemenea

  • is_bool() - Determină dacă o variablă este un boolean
  • is_int() - Determină dacă tipul unei variabile este integer
  • is_float() - Determină dacă tipul unei variabile este float
  • is_string() - Determină dacă tipul variabilei este string
  • is_array() - Determină dacă o variabilă este un array



is_real> <is_numeric
Last updated: Fri, 20 Nov 2009
 
add a note add a note User Contributed Notes
is_object
ldean at saleamp dot com
01-Sep-2009 02:16
Use instanceof() to check for a specific type.
Senthryl
17-Mar-2009 02:03
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)));
}
?>
gregdangelo at gmail dot com
14-Apr-2008 07:03
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
20-May-2006 08:03
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
03-Feb-2005 01:06
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.
victor AT fourstones DOT net
01-Jan-2005 11:49
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
01-Jan-2005 03:47
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!
lbjay can be emailed at reallywow dot com
02-May-2003 07:18
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.

is_real> <is_numeric
Last updated: Fri, 20 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites