Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether property exists in object or class with PHP
The property_exists() or the isset() function can be used to check if the property exists in the class or object.
Syntax
Below is the syntax of property_exists() function−
property_exists( mixed $class , string $property )
Example
if (property_exists($object, 'a_property'))
Below is the syntax of isset() function−
isset( mixed $var [, mixed $... ] )
Example
if (isset($object->a_property))
The isset() will return false if the ‘a_property’ is null.
Example
Let us see an example −
<?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
This will produce the following output−
bool(true) bool(true)
Advertisements