略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: property_exists

2024-12-22

property_exists

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

property_exists检查对象或类是否具有该属性

说明

property_exists(mixed $class, string $property): bool

本函数检查给出的 property 是否存在于指定的类中(以及是否能在当前范围内访问)。

注意:

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

参数

class

字符串形式的类名或要检查的类的一个对象

property

属性的名字

返回值

如果该属性存在则返回 true,如果不存在则返回 false,出错返回 null

注释

注意:

如果此类不是已知类,使用此函数会使用任何已注册的 autoloader

注意:

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

更新日志

版本 说明
5.3.0 This function checks the existence of a property independent of accessibility.

范例

示例 #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();

?>

参见

add a noteadd a note

User Contributed Notes 10 notes

up
59
g dot gentile at parentesigraffe dot com
7 years 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
21
Stefan W
8 years 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
11
Nanhe Kumar
8 years 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
saurabh dot agarwal89 at gmail dot com
7 years ago
$a = array('a','b'=>'c');
print_r((object) $a);
var_dump( property_exists((object) $a,'0'));
var_dump( property_exists((object) $a,'b'));

OUTPUT:
stdClass Object
(
    [0] => a
    [b] => c
)
bool(false)
bool(true)
up
0
rnealxp at yahoo dot com
2 years ago
For class constants, use defined() to check for their existence since property_exists() cannot be used.
<?php
class A{
    private static
$c = 'C';
    const
B = 'B';
}
if(
property_exists('A','c')===true){
    echo
'y';
}else{
    echo
'n';
}
//output: y
if(property_exists('A','B')===true){
    echo
'y';
}else{
    echo
'n';
}
//output: n
if(defined('A::B')===true){
    echo
'y';
}else{
    echo
'n';
}
//output: y
?>
up
0
@fitorec
2 years ago
<?php

abstract class P {
  private
$priv;
  static protected
$static_prop;
  static function
exists($prop = 'priv') {
     
var_dump(property_exists(new static, $prop)); //true
 
}
}

class
S extends P {
  static protected
$new_prop;
}
S::exists('new_prop');       // True
S::exists('static_prop');    // True
S::exists('priv');           // True
up
0
falundir at gmail dot com
5 years ago
If you want to test if declared *public* property was unset, you can use the following code:

<?php
class A {
    public
$declared;
}

$a = new A();
$is_defined = array_key_exists('declared', (array)$a); //=>true

unset($a->declared);
$is_defined = array_key_exists('declared', (array)$a); //=>false
?>
up
0
ewisuri [gmail]
8 years 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
-3
Daniel dot Peder at infoset dot com
5 years ago
declared properties cannot be unset
any set property does exist, even being set to null, regardless how it was set

<?php

class demo_property_exists {

    public
$my_public;
    protected
$my_protected;
    private
$my_private;

    function
__construct() {
       
$this->dumper('before-constructed');
       
$this->my_constructed_int = 123;
       
$this->my_constructed_null = null;
       
$this->dumper('after-constructed');
    }

    public function
dumper($name) {
       
printf("\n[%s] dump:\n", $name);

        foreach (
$this->my_checklist() as $prop) {
           
printf("[%s]:\t", $prop);
           
var_dump(property_exists($this, $prop));
        }
    }

    public function
unset_all() {
        foreach (
$this->my_checklist() as $prop) {
            unset(
$this->$prop);
        }
    }

    private function
my_checklist() {
        return array(
'my_public', 'my_protected', 'my_private', 'my_constructed_int', 'my_constructed_null', 'my_assigned_int', 'my_assigned_null',);
    }

}

$object = new demo_property_exists();
$object->dumper('before-assigned');
$object->my_assigned_int = 456;
$object->my_assigned_null = null;
$object->dumper('after-assigned');
$object->unset_all();
$object->dumper('after-unset');

/*
results:

[before-constructed] dump:
[my_public]:    bool(true)
[my_protected]: bool(true)
[my_private]:   bool(true)
[my_constructed_int]:   bool(false)
[my_constructed_null]:  bool(false)
[my_assigned_int]:      bool(false)
[my_assigned_null]:     bool(false)

[after-constructed] dump:
[my_public]:    bool(true)
[my_protected]: bool(true)
[my_private]:   bool(true)
[my_constructed_int]:   bool(true)
[my_constructed_null]:  bool(true)
[my_assigned_int]:      bool(false)
[my_assigned_null]:     bool(false)

[before-assigned] dump:
[my_public]:    bool(true)
[my_protected]: bool(true)
[my_private]:   bool(true)
[my_constructed_int]:   bool(true)
[my_constructed_null]:  bool(true)
[my_assigned_int]:      bool(false)
[my_assigned_null]:     bool(false)

[after-assigned] dump:
[my_public]:    bool(true)
[my_protected]: bool(true)
[my_private]:   bool(true)
[my_constructed_int]:   bool(true)
[my_constructed_null]:  bool(true)
[my_assigned_int]:      bool(true)
[my_assigned_null]:     bool(true)

[after-unset] dump:
[my_public]:    bool(true)
[my_protected]: bool(true)
[my_private]:   bool(true)
[my_constructed_int]:   bool(false)
[my_constructed_null]:  bool(false)
[my_assigned_int]:      bool(false)
[my_assigned_null]:     bool(false)

*/
up
-8
David Spector
3 years ago
Just to clarify, property_exists only works for static properties. There is no corresponding const_exists function in PHP to do something similar for const names.

官方地址:https://www.php.net/manual/en/function.property-exists.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3