Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> PHP

PHP में Object/Class में Property Exists Check करना


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 valuetruetrue
Public, null valuetruefalse
Private/Protected (accessible scope)truetrue
Private/Protected (outside scope)truefalse
Non-existent propertyfalsefalse
Magic __get() propertyfalse*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";
}
  1. क्या हम JavaScript में जांच सकते हैं कि कोई प्रॉपर्टी ऑब्जेक्ट में है या नहीं? क्या हम JavaScript में जांच सकते हैं कि कोई प्रॉपर्टी ऑब्जेक्ट में है या नहीं?

    JavaScript में जांचने का कोड कि कोई प्रॉपर्टी ऑब्जेक्ट में मौजूद है या नहीं:उदाहरण<!DOCTYPE html> <html lang="hi"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title&

  1. JavaScript का उपयोग करके कैसे जांचें कि बटन क्लिक किया गया है? JavaScript का उपयोग करके कैसे जांचें कि बटन क्लिक किया गया है?

    JavaScript का उपयोग करके जांचने का कोड कि बटन क्लिक किया गया है या नहीं:उदाहरण<!DOCTYPE html> <html lang="hi"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>B

  1. जावास्क्रिप्ट में कैसे जांचें कि कोई ऑब्जेक्ट क्लास का इंस्टेंस है? जावास्क्रिप्ट में कैसे जांचें कि कोई ऑब्जेक्ट क्लास का इंस्टेंस है?

    जावास्क्रिप्ट में जांचने का कोड कि कोई ऑब्जेक्ट क्लास का इंस्टेंस है या नहीं - उदाहरण <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8 /> <meta name=viewport content=width=device-width, initial-scale=1.0 /> <title>Document</title> <style> &n