property_exists

(PHP 5 >= 5.1.0)

property_exists Prüft auf die Existenz einer Eigenschaft eines Objektes bzw. einer Klasse

Beschreibung

bool property_exists ( mixed $class , string $property )

Prüft ob die Eigenschaft property in der angegebenen Klasse existiert.

Hinweis:

Anders als isset() gibt property_exists() auch dann TRUE zurück wenn eine Eigenschaft den Wert NULL hat.

Parameter-Liste

class

Objektinstanz oder Name einer Klasse.

property

Name der zu prüfenden Eigenschaft.

Rückgabewerte

Gibt TRUE zurück wenn die Eigenschaft existiert, FALSE wenn nicht, und NULL im Fehlerfall.

Anmerkungen

Hinweis:

Die Verwendung dieser Funktion wird jegliche registrierte Autoloader verwenden, falls die Klasse nicht bereits bekannt ist.

Hinweis:

Die property_exists() Funktion kann keine Eigenschaften erkennen die über die 'magische' Methode __get implementiert sind.

Changelog

Version Beschreibung
5.3.0 Die Funktion prüft auf die Existenz der Eigenschaft unabhängig von den Zugriffsberechtigungen.

Beispiele

Beispiel #1 Ein property_exists() Beispiel

<?php

class myClass {
    public 
$mine;
    private 
$xpto;
    static protected 
$test;

    static function 
test() {
        
var_dump(property_exists('myClass''xpto')); //true
    
}
}

var_dump(property_exists('myClass''mine'));   //true
var_dump(property_exists(new myClass'mine')); //true
var_dump(property_exists('myClass''xpto'));   //true  ab PHP 5.3.0
var_dump(property_exists('myClass''bar'));    //false
var_dump(property_exists('myClass''test'));   //true ab PHP 5.3.0
myClass::test();

?>

Siehe auch

  • method_exists() - Prüft ob eine Methode innerhalb eines Objekts existiert

add a note add a note

User Contributed Notes 5 notes

up
10
Stefan W
1 year ago
If you are in a namespaced file, and you want to pass the class name as a string, you will have to include the full namespace for the class name - even from inside the same namespace:

<?
namespace MyNS;

class
A {
    public
$foo;
}

property_exists("A", "foo");          // false
property_exists("\\MyNS\\A", "foo");  // true
?>
up
5
Nanhe Kumar
1 year ago
<?php

class Student {

    protected
$_name;
    protected
$_email;
   

    public function
__call($name, $arguments) {
       
$action = substr($name, 0, 3);
        switch (
$action) {
            case
'get':
               
$property = '_' . strtolower(substr($name, 3));
                if(
property_exists($this,$property)){
                    return
$this->{$property};
                }else{
                    echo
"Undefined Property";
                }
                break;
            case
'set':
               
$property = '_' . strtolower(substr($name, 3));
                if(
property_exists($this,$property)){
                   
$this->{$property} = $arguments[0];
                }else{
                    echo
"Undefined Property";
                }
               
                break;
            default :
                return
FALSE;
        }
    }

}

$s = new Student();
$s->setName('Nanhe Kumar');
$s->setEmail('nanhe.kumar@gmail.com');
echo
$s->getName(); //Nanhe Kumar
echo $s->getEmail(); // nanhe.kumar@gmail.com
$s->setAge(10); //Undefined Property
?>
up
2
ewisuri [gmail]
1 year ago
As of PHP 5.3.0, calling property_exists from a parent class sees private properties in sub-classes.

<?php
class P {
    public function
test_prop($prop) { return property_exists($this, $prop); }
}

class
Child extends P {
    private
$prop1;
}

$child = new Child();
var_dump($child->test_prop('prop1')); //true, as of PHP 5.3.0
up
1
g dot gentile at parentesigraffe dot com
25 days ago
The function behaves differently depending on whether the property has been present in the class declaration, or has been added dynamically, if the variable has been unset()

<?php

class TestClass {

    public
$declared = null;
   
}

$testObject = new TestClass;

var_dump(property_exists("TestClass", "dynamic")); // boolean false, as expected
var_dump(property_exists($testObject, "dynamic")); // boolean false, same as above

$testObject->dynamic = null;
var_dump(property_exists($testObject, "dynamic")); // boolean true

unset($testObject->dynamic);
var_dump(property_exists($testObject, "dynamic")); // boolean false, again.

var_dump(property_exists($testObject, "declared")); // boolean true, as espected

unset($testObject->declared);
var_dump(property_exists($testObject, "declared")); // boolean true, even if has been unset()
up
-3
berimbolo
1 year ago
When you are testing for a property that has been added dynamically, you will need to provide a reference to the instance rather than the class name for the first parameter.

For example, for this code snippet:

class myClass {
public $var;
}

$obj = new myClass;
$obj->new_var = "new";

var_dump(property_exists('myClass', 'new_var') );
var_dump(property_exists($obj, 'new_var') );

Returns:
boolean false
boolean true
To Top