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
property_exists
(PHP 5 >= 5.1.0)
property_exists — Checks if the object or class has a property
Popis
bool property_exists ( mixed $class, string $property )This function checks if the given property exists in the specified class (and if it is accessible from the current scope).
Poznámka: As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.
Seznam parametrů
- class
The class name or an object of the class to test for
- property
The name of the property
Návratové hodnoty
Returns TRUE if the property exists, FALSE if it doesn't exist or NULL in case of an error.
Příklady
Příklad 284. 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();
?>
Viz také
| method_exists() |
property_exists
K
13-Jul-2007 01:11
13-Jul-2007 01:11
ssettl2 at google's mail
30-Mar-2007 12:37
30-Mar-2007 12: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 10:57
07-Aug-2006 10: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 02:35
08-Jun-2006 02: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 11:52
01-Jun-2006 11: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
14-Nov-2005 04:20
14-Nov-2005 04: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));
}
}
?>
