A Guide to the PHP property_exists() function by Practical Examples (original) (raw)
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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>o</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><msub><mi>t</mi><mi>o</mi></msub><msub><mi>r</mi><mi>c</mi></msub><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">object_or_class, string </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.05724em;">bj</span><span class="mord mathnormal">ec</span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">o</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">c</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">a</span><span class="mord mathnormal">ss</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>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.
The following example uses the property_exists() function to check if the FileReader class has a specific property:
`<?php
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:
`<?php
abstract class Model { public function load(array $data): self { foreach ($data as key=>key => key=>value) { if (property_exists($this, $key)) { this−>this->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' => '[[email protected]](/cdn-cgi/l/email-protection)', '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.
Did you find this tutorial useful?