note 74230 added to function.property-exists
| From: | ssettl2atgoogle | Date: | Fri, 30 Mar 2007 19:37:51 +0000 |
| Subject: | note 74230 added to function.property-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124961@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 69.39.81.135
Probable Submitter: 128.175.51.191
----
Manual Page -- http://www.php.net/manual/en/function.property-exists.php
Edit -- https://master.php.net/note/edit/74230
Del: integrated -- https://master.php.net/note/delete/74230/integrated
Del: useless -- https://master.php.net/note/delete/74230/useless
Del: bad code -- https://master.php.net/note/delete/74230/bad+code
Del: spam -- https://master.php.net/note/delete/74230/spam
Del: non-english -- https://master.php.net/note/delete/74230/non-english
Del: in docs -- https://master.php.net/note/delete/74230/in+docs
Del: other reasons-- https://master.php.net/note/delete/74230
Reject -- https://master.php.net/note/reject/74230
Search -- https://master.php.net/manage/user-notes.php