property_exists

Checks if the object or class has a property

Syntax

property_exists ( mixed $class , string $property ) : bool

Parameters

class

The class name or an object of the class to test for

property

The name of the property

Return

Returns TRUE if the property exists, FALSE if it doesn't exist or NULL in case of an error.

Examples

1 · Object

<?

class myclass
{
    public $var1;
    protected $var2;
    private $var3;
    static $var4;
}

$class = new myclass();
$property1 = "var1";
$property2 = "var2";
$property3 = "var3";
$property4 = "var4";

$return1 = property_exists($class, $property1);
$return2 = property_exists($class, $property2);
$return3 = property_exists($class, $property3);
$return4 = property_exists($class, $property4);

var_dump($return1, $return2, $return3, $return4);

?>
bool(true)
bool(true)
bool(true)
bool(true)

2 · Class Name

<?

class myclass
{
    public $var1;
    protected $var2;
    private $var3;
    static $var4;
}

$class = "myclass";
$property1 = "var1";
$property2 = "var2";
$property3 = "var3";
$property4 = "var4";

$return1 = property_exists($class, $property1);
$return2 = property_exists($class, $property2);
$return3 = property_exists($class, $property3);
$return4 = property_exists($class, $property4);

var_dump($return1, $return2, $return3, $return4);

?>
bool(true)
bool(true)
bool(true)
bool(true)

3 · namespace

<?

namespace mynamespace;

class myclass
{
    public $var1;
    protected $var2;
    private $var3;
    static $var4;
}

$class = "mynamespace\myclass";
$property1 = "var1";
$property2 = "var2";
$property3 = "var3";
$property4 = "var4";

$return1 = property_exists($class, $property1);
$return2 = property_exists($class, $property2);
$return3 = property_exists($class, $property3);
$return4 = property_exists($class, $property4);

var_dump($return1, $return2, $return3, $return4);

?>
bool(true)
bool(true)
bool(true)
bool(true)

class_alias

class_exists

get_called_class

get_class

get_class_methods

get_class_vars

get_declared_classes

get_declared_interfaces

get_declared_traits

get_object_vars

get_parent_class

interface_exists

is_a

is_subclass_of

method_exists

trait_exists

PHP

Home Menu