property_exists() function in PHP


The property_exists() method checks if the object or class has a property.

Syntax

property_exists(object, property)

Parameters

  • object/ class − The object or the class name

  • property − The name of the property

Return

The property_exists() function returns TRUE if the property exists, FALSE if it doesn't exist or NULL in case of an error.

Example

The following is an example −

 Live Demo

<?php
   class Demo {
      public $one;
      private $two;
      static protected $VAL;
      static function VAL() {
         var_dump(property_exists('myClass', 'two'));
      }
   }
   var_dump(property_exists('Demo', 'one'));
   var_dump(property_exists(new Demo, 'one'));
?>

Output

The following is the output −

bool(true)
bool(true)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Dec-2019

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements