PHP में object या class में property की उपस्थिति जांचने के दो मुख्य तरीके: property_exists() और isset()।
property_exists() - Declaration Check
Property class में declared है या नहीं जांचता है, भले ही value null हो।
<?php
class User {
public $name = 'अमित';
public $email = null; // Declared but null
private $password = 'secret';
protected $createdAt;
const ROLE = 'user';
}
$user = new User();
// Object पर
var_dump(property_exists($user, 'name')); // true
var_dump(property_exists($user, 'email')); // true (null लेकिन declared)
var_dump(property_exists($user, 'password')); // true (private भी)
var_dump(property_exists($user, 'nonexistent')); // false
// Class name पर (static check)
var_dump(property_exists('User', 'name')); // true
var_dump(property_exists('User', 'ROLE')); // false (constants नहीं)
isset() - Value Check
Property set है और null नहीं है जांचता है।
<?php $user = new User(); var_dump(isset($user->name)); // true (set and not null) var_dump(isset($user->email)); // false (null है!) var_dump(isset($user->password)); // false (private - inaccessible) var_dump(isset($user->nonexistent)); // false
तुलना तालिका
| स्थिति | property_exists() | isset() |
|---|---|---|
| Public, non-null value | true | true |
| Public, null value | true | false |
| Private/Protected (accessible scope) | true | true |
| Private/Protected (outside scope) | true | false |
| Non-existent property | false | false |
| Magic __get() property | false* | true** |
* property_exists() magic properties detect नहीं करता जब तक __isset() implement न हो।
** isset() __isset() magic method को trigger करता है।
Magic Methods के साथ
<?php
class DynamicProperties {
private $data = [];
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __get($name) {
return $this->data[$name] ?? null;
}
public function __isset($name) {
return isset($this->data[$name]);
}
}
$obj = new DynamicProperties();
$obj->dynamic = 'value';
var_dump(property_exists($obj, 'dynamic')); // false (no __isset implementation for property_exists)
var_dump(isset($obj->dynamic)); // true (__isset called)
व्यावहारिक उपयोग: Safe Property Access
<?php
function getPropertySafe($object, string $property, $default = null) {
// Option 1: property_exists + null check
if (property_exists($object, $property)) {
$value = $object->$property;
return $value !== null ? $value : $default;
}
return $default;
// Option 2: isset (simpler, लेकिन null values miss करता है)
// return isset($object->$property) ? $object->$property : $default;
// Option 3: Null coalescing (PHP 7+) - simplest for public properties
// return $object->$property ?? $default; // Notice if property doesn't exist
}
// Better: use reflection for complete check
function hasProperty($object, string $property): bool {
$reflection = new ReflectionObject($object);
return $reflection->hasProperty($property);
}
Reflection के साथ Complete Check
<?php
class Example {
public $public = 'public';
protected $protected = 'protected';
private $private = 'private';
public static $static = 'static';
}
$obj = new Example();
$reflection = new ReflectionClass($obj);
echo "All properties:\n";
foreach ($reflection->getProperties() as $prop) {
echo " {$prop->getName()} - " .
($prop->isPublic() ? 'public' : ($prop->isProtected() ? 'protected' : 'private')) . "\n";
}
// Static properties
echo "Static properties:\n";
foreach ($reflection->getStaticProperties() as $name => $value) {
echo " $name = $value\n";
}