HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

property_exists

Description

The property_exists of Class / Object for PHP checks if the object or class has a property.

Syntax

 property_exists(
    object|string $object_or_class,
    string $property
): bool

Parameters

object_or_class

The class name or an object of the class

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

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

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

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

2 · class

<?

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

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

$return1 = property_exists($object_or_class, $property1);
$return2 = property_exists($object_or_class, $property2);
$return3 = property_exists($object_or_class, $property3);
$return4 = property_exists($object_or_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;
}

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

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

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