property_exists

(PHP 5 >= 5.1.0RC1)

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

说明

bool property_exists ( mixed class, string property )

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

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

参数

class

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

property

属性的名字

返回值

如果该属性存在则返回 TRUE,如果不存在则返回 FALSE,出错返回 NULL

范例

例 1. property_exists() 例子

<?php

class myClass {
    
public $mine;
    
private $xpto;

    static function
test() {
        
var_dump(property_exists('myClass', 'xpto')); // true, it can be accessed from here
    
}
}

var_dump(property_exists('myClass', 'mine'));   //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto'));   //false, isn't public
myClass::test();
?>


add a note add a note User Contributed Notes
K
13-Jul-2007 08:11
to ssettl2 at google's mail >

1) Use self:: in static methods : http://php.net/language.oop5.static

2) Under the description title of this page : "This function checks if the given property exists in the specified class (and if it is ACCESSIBLE FROM THE CURRENT SCOPE)."

This is normal behaviour in PHP5.

Regards
ssettl2 at google's mail
30-Mar-2007 07:37
Something interesting that I've run into that I don't see discussed on here.

property_exists doesn't like being called with $this inside of a static method.

<?
// base class from which all inherit (for laziness)
abstract class object
{
      
// static so it's not overwritten
      
public static function setMember($member,$value){
               if(
property_exists($this,$member)){  // doesn't like $this
                      
$this->$member = $value;
               }else{
                       die(
"$member is not a property in ".get_class($this)."<br>");
                       return(
false);
               }
       }
}

// extend it with a basic test class
class test extends object{
   protected
$test1;
   protected
$test2;

}
?>
then
<?
$test
= new test();
$test->setMember('test1',1);
/* dies here with:
Warning: First parameter must either be an object or the name of an existing class
Pointing to the property_exists call
*/
?>

Another thing:

property_exists returns false when called on a private member using $this as the object.

ie:
<?
abstract class object
{
      
// not static this time
      
public function setMember($member,$value){
               if(
property_exists($this,$member)){ // trouble with $this
                      
$this->$member = $value;
               }else{
                       echo(
"$member is not a property in ".get_class($this)."<br>");
                       return(
false);
               }
       }
}

// extend it with a basic test class
class test extends object{
   private
$test1;      // private will cause false with $this
  
protected $test2// protected works as expected

}

$test = new test();
$test->setMember('test1',1); // fails
$test->setMember('test2',2); // succeeds

?>
Alan71
07-Aug-2006 05:57
This function is case-sensitive, so :

<?php
class Test {
   public
$property;
  
   public
foo() { echo($property); }
}

property_exists('Test', 'property');  // will return true
property_exists('Test', 'Property');  // will return false
?>

(under PHP5.1.2)
jcaplan at bogus dot amazon dot com
08-Jun-2006 09:35
The documentation leaves out the important case of new properties you add to objects at run time.  In fact, property_exists will return true if you ask it about such properties.

<?
class Y {}
$y = new Y;

echo isset(
$y->prop ) ? "yes\n" : "no\n"; // no;
echo property_exists( 'Y', 'prop' ) ? "yes\n" : "no\n"; // no
echo property_exists( $y, 'prop' ) ? "yes\n" : "no\n"; // no

$y->prop = null;

echo isset(
$y->prop ) ? "yes\n" : "no\n"; // no;
echo property_exists( 'Y', 'prop' ) ? "yes\n" : "no\n"; // no
echo property_exists( $y, 'prop' ) ? "yes\n" : "no\n"; // yes
?>
Pete W
01-Jun-2006 06:52
In a similar vein to the previous note, To check in PHP4 if an object has a property, even if the property is null:

<?php

if(array_key_exists('propertyName',get_object_vars($myObj)))
{
 
// ..the property has been defined

?>
timshel
15-Nov-2005 12:20
I haven't tested this with the exact function semantics of 5.1, but this code should implement this function in php < 5.1:

<?php
if (!function_exists('property_exists')) {
  function
property_exists($class, $property) {
   if (
is_object($class))
    
$class = get_class($class);

   return
array_key_exists($property, get_class_vars($class));
  }
}
?>