PHP property_exists
Summary: in this tutorial, you’ll learn how to use the property_exists() function to check if the object or class has a property.
Introduction to the PHP property_exists function
The property_exists() function returns true if an object or a class has a property. Otherwise, it returns false.
Here’s the syntax of the property_exists() method:
property_exists(object|string $object_or_class, string $property): boolCode language: PHP (php)
The property_exists() method has two parameters:
- The
$object_or_classspecifies the object or class that needs to check for the existence of a property. - The
$propertyspecifies the name of the property to check.
Note that in case of an error, the property_exists() function returns null instead.
PHP property_exists function examples
The following example uses the property_exists() function to check if the FileReader class has a specific property:
class FileReader
{
private $filename;
public $done;
protected $filesize;
public static $mimeTypes;
}
var_dump(property_exists(FileReader::class, 'filename')); // true
var_dump(property_exists(FileReader::class, 'done')); // true
var_dump(property_exists(FileReader::class, 'filesize')); // true
var_dump(property_exists(FileReader::class, 'mimeTypes')); // true
var_dump(property_exists(FileReader::class, 'status')); // false
Code language: PHP (php)
PHP property_exists function practical example
Suppose that you have a base class called Model. All the model classes need to extend this Model class.
To load a Model object from an associative array, you can define a load() method in the Model class as follows:
abstract class Model
{
public function load(array $data): self
{
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
}
return $this;
}
}
Code language: PHP (php)
The load() method accepts an associative array as an argument. It iterates over the array element. If the object has a property that matches a key in the array, the load() method assigns the value to the property.
The following defines the User class that extends the Model class:
class User extends Model
{
private $username; private $email;
private $password;
}
Code language: PHP (php)
To populate the properties of the User class with values of an array, you call the load() method like this:
$user = (new User())->load([
'username' => 'john',
'email' => 'john@phptutorial.net',
'password' => password_hash('VerySecurePa$$1.', PASSWORD_DEFAULT),
]);Code language: PHP (php)
In practice, you would have a registration form. After the form is submitted, you need to validate the data in the $_POST array. And then you call the load() method to initialize a User object.
Summary
- Use the PHP
property_exists()function to check if an object or a class has a specific property.