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.p
hp
Edit -- https://master
.php.net/note/edit/74230
Del: integrated -- h
ttps://master.php.net/note/delete/74230/integrated
Del: useless -- http
s://master.php.net/note/delete/74230/useless
Del: bad code -- htt
ps://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 -- http
s://master.php.net/note/delete/74230/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/74230
Reject -- https://mast
er.php.net/note/reject/74230
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|