note 79111 added to function.property-exists
| From: | rayro at gmx dot de | Date: | Mon, 12 Nov 2007 00:49:37 +0000 |
| Subject: | note 79111 added to function.property-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132191@lists.php.net to get a copy of this message | ||
To check the existance of a property from outside the scope (even if it's not accessible)
try/consider the following:
<?php
function property_exists_safe($class, $prop)
{
$r = property_exists($class, $prop);
if (!$r) {
$x = new ReflectionClass($class);
$r = $x->hasProperty($prop);
}
return $r;
}
class myClass {
public $mine;
private $xpto;
static function test1() {
// true, it can be accessed from here
var_dump(property_exists('myClass', 'xpto'));
}
static function test2() {
// true, it can be accessed from everywhere!
var_dump(property_exists_safe('myClass', 'xpto'));
}
}
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::test1();
echo("\n");
var_dump(property_exists_safe('myClass', 'mine')); //true
var_dump(property_exists_safe(new myClass, 'mine')); //true
var_dump(property_exists_safe('myClass', 'xpto')); //true
myClass::test2(); //true
?>
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
----
Server IP: 217.13.201.10
Probable Submitter: 84.135.106.46
----
Manual Page -- http://www.php.net/manual/en/function.property-exists.php
Edit -- https://master.php.net/note/edit/79111
Del: integrated -- https://master.php.net/note/delete/79111/integrated
Del: useless -- https://master.php.net/note/delete/79111/useless
Del: bad code -- https://master.php.net/note/delete/79111/bad+code
Del: spam -- https://master.php.net/note/delete/79111/spam
Del: non-english -- https://master.php.net/note/delete/79111/non-english
Del: in docs -- https://master.php.net/note/delete/79111/in+docs
Del: other reasons-- https://master.php.net/note/delete/79111
Reject -- https://master.php.net/note/reject/79111
Search -- https://master.php.net/manage/user-notes.php