PHP Function Reference

PHP property_exists() Function



The PHP property_exists() function is used to check if the given property exists in the specified class. It returns true if the property exists, false if it doesn't exist or null in case of an error.

Note: As opposed with isset(), property_exists() returns true even if the property has the value null.

Syntax

property_exists(object_or_class, property)

Parameters

object_or_class Required. Specify the class name or an object of the class to check for.
property Required. Specify the name of the property.

Return Value

Returns true if the property exists, false if it doesn't exist or null in case of an error.

Example: property_exists() example

The example below shows the usage of property_exists() function.

<?php
class myClass {
  public $mine;
  private $xpto;
  static protected $test;

  static function test() {
    var_dump(property_exists('myClass', 'xpto')); //true
  }
}

var_dump(property_exists('myClass', 'mine'));   //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto'));   //true
var_dump(property_exists('myClass', 'bar'));    //false
var_dump(property_exists('myClass', 'test'));   //true
myClass::test();
?>

The output of the above code will be:

bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)

❮ PHP Classes/Objects Reference