suche nach in der

trait_exists> <method_exists
Last updated: Fri, 18 May 2012

view this page in

property_exists

(PHP 5 >= 5.1.0)

property_exists Prüft auf die Existenz einer Eigenschaft eiens 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 on eine Methode innerhalb eines Objekts existiert



add a note add a note User Contributed Notes
property_exists
K
13-Jul-2007 10: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 09: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 07: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 11: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 08: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 01: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));
  }
}
?>

trait_exists> <method_exists
Last updated: Fri, 18 May 2012