property_exists

(PHP 5 >= 5.1.0)

property_exists Checks if the object or class has a property

Descrizione

bool property_exists ( mixed $class , string $property )

This function checks if the given property exists in the specified class.

Nota:

As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

Elenco dei parametri

class

The class name or an object of the class to test for

property

The name of the property

Valori restituiti

Returns TRUE if the property exists, FALSE if it doesn't exist or NULL in case of an error.

Note

Nota:

L'utilizzo di questa funzione attiverà tutti gli autoloaders se la classe non è già conosciuta.

Nota:

The property_exists() function cannot detect properties that are magically accessible using the __get magic method.

Log delle modifiche

Versione Descrizione
5.3.0 This function checks the existence of a property independent of accessibility.

Esempi

Example #1 A property_exists() example

<?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, as of PHP 5.3.0
var_dump(property_exists('myClass''bar'));    //false
var_dump(property_exists('myClass''test'));   //true, as of PHP 5.3.0
myClass::test();

?>

Vedere anche:

add a note add a note

User Contributed Notes 4 notes

up
2
Stefan W
7 months 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
2
Nanhe Kumar
3 months 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
1
ewisuri [gmail]
2 months 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
berimbolo
2 months 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