Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Class/Object Functions : property_exists

property_exists

Checks if the object or class has a property (PHP 5 >= 5.1.0)
bool property_exists ( mixed class, string property )

Example 388. A property_exists() example

<?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();

?>

Code Examples / Notes » property_exists

k

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


alan71

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

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
?>


ssettl2

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)."
");
                       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)."
");
                       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
?>


pete w

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

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));
 }
}
?>


caist

A niet way to copy properties from one object to another avoiding stuff like:
$obj2 -> prop1 = $obj1 -> prop1;
$obj2 -> prop2 = $obj1 -> prop2;
$obj2 -> prop3 = $obj1 -> prop3;
...is this loop through all properties:
// copies all property values from obj1 to obj2
foreach ($obj1 as $prop_name => $prop_value)
{
if (property_exists(get_class($obj2), $prop_name))
                $obj2 -> {$prop_name} = $prop_value;
}


Change Language


Follow Navioo On Twitter
call_user_method_array
call_user_method
class_exists
get_class_methods
get_class_vars
get_class
get_declared_classes
get_declared_interfaces
get_object_vars
get_parent_class
interface_exists
is_a
is_subclass_of
method_exists
property_exists
eXTReMe Tracker