Current Location: Home> Latest Articles> How to Use is_a() and property_exists() Functions to Determine Object Property Access Safety?

How to Use is_a() and property_exists() Functions to Determine Object Property Access Safety?

M66 2025-06-30

1. is_a() Function Introduction

is_a() is used to determine if an object is an instance of a specified class or a subclass of that class. The function signature is as follows:

bool is_a(object $object, string $class_name, bool $allow_string = false)  
  • $object: The object to be checked.

  • $class_name: The name of the class as a string.

  • $allow_string (PHP 7.2+): Allows passing the class name as a string instead of an object (rarely used).

Example:

class Animal {}  
class Dog extends Animal {}
<p>$dog = new Dog();</p>
<p>if (is_a($dog, 'Animal')) {<br>
echo "This is an object of class Animal or its subclass";<br>
}<br>

Here, is_a() returns true because Dog is a subclass of Animal.

2. property_exists() Function Introduction

property_exists() is used to check if an object has a specific property, regardless of whether the property is public, protected, or private. The function signature is:

bool property_exists(object|string $class, string $property)  
  • $class: The object or class name as a string.

  • $property: The property name as a string.

Example:

class Person {  
    private $name;  
    public $age;  
}
<p>$p = new Person();</p>
<p>if (property_exists($p, 'name')) {<br>
echo "Object has property name";<br>
}</p>
<p>if (property_exists($p, 'age')) {<br>
echo "Object has property age";<br>
}<br>

3. Combined Usage to Determine Object Property Access Safety

Using property_exists() alone does not tell you whether the code has access to that property, because even private or protected properties return true. On the other hand, is_a() can help determine the class of the object, allowing you to evaluate access rights based on the class structure. However, PHP does not provide a direct function to assess the "access safety" of a property—i.e., whether the current code has permission to access the property. The usual approach is as follows:
  • First, use is_a() to check whether the object belongs to a controllable class range.

  • Then use property_exists() to check whether the property exists.

  • Finally, use reflection or set access interfaces during design to ensure property access safety.

4. Code Example

<?php  
class User {  
    public $username;  
    protected $email;  
    private $password;  
    $this->username = $username;  
    $this->email = $email;  
    $this->password = $password;  
}  

}

function checkPropertyAccess($obj, $property, $allowedClass) {
// Check if the object is an instance of an allowed class or its subclass
if (!is_a($obj, $allowedClass)) {
return false;
}

// Check if the property exists  
if (!property_exists($obj, $property)) {  
    return false;  
}  

// Use reflection to check the visibility of the property  
$reflection = new ReflectionObject($obj);  
if (!$reflection->hasProperty($property)) {  
    return false;  
}  

$prop = $reflection->getProperty($property);  

// Check if the property is public  
if ($prop->isPublic()) {  
    return true;  
}  

// If not public, it is not directly accessible, so access is restricted for safety  
return false;  

}

$user = new User('alice', 'alice@example.com', 'secret');

var_dump(checkPropertyAccess($user, 'username', 'User')); // true
var_dump(checkPropertyAccess($user, 'email', 'User')); // false
var_dump(checkPropertyAccess($user, 'password', 'User')); // false
var_dump(checkPropertyAccess($user, 'nonexist', 'User')); // false
var_dump(checkPropertyAccess(new stdClass(), 'username', 'User')); // false

5. Conclusion

  • is_a() is used to check if an object belongs to a class or one of its subclasses, ensuring safe access scope.

  • property_exists() is used to detect if an object has a specified property.

  • By combining reflection, you can further check the access permissions of a property, preventing illegal access to private or protected properties.

  • In actual development, to ensure property access safety, it is recommended to expose properties through encapsulation (e.g., getter/setter methods) rather than direct access.

By properly using these functions, you can significantly enhance the robustness and security of your code, reducing runtime errors and potential security risks.