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