1
我正在研究我的應用程序,發現了靜態調用但未定義爲擴展相同類的靜態方法的奇怪行爲。最終,這個方法可以訪問和修改調用者保護的變量和方法。 這裏是例如我的代碼:靜態方法可以訪問調用者對象,錯誤或功能?
<?php
class object
{
private $version;
protected $alteredBy = 'nobody';
public function __construct()
{
$this->version = PHP_VERSION;
$this->objectName = get_class($this);
echo sprintf("<pre><strong>New %s Created</strong>", $this->objectName);
}
public function __destruct()
{
echo sprintf("</pre><strong>Source Code</strong><div>%s</div>", highlight_file(__FILE__, true));
}
}
class superApplication extends object
{
public function __toString()
{
echo "\nCalling third party object statically like thirdParty::method()\n";
echo thirdParty::method();
echo "\nCalling third party object statically via call_user_func()\n";
echo call_user_func(array('thirdParty','method'));
echo sprintf("New Object params\n%s", print_r($this, true));
return sprintf("%s: done\n", $this->objectName);
}
}
class thirdParty extends object
{
public function method()
{
if(is_object($this))
{
$this->alteredBy = __CLASS__;
return sprintf(
"<span style=\"color:red\">Object '%s' was altered successfully by %s class</span>\n",
get_class($this),
__CLASS__
);
}
else return "Cannot access caller object\n\n";
}
}
print new superApplication;
?>
此行爲沒有記錄,所以我不知道是不是錯誤或功能,並可以將它導致的安全問題?
更新。 我知道,$這是不允許的靜態方法內,這種行爲出現在PHP版本5.2.11
<評論刪除> – 2010-09-12 21:54:16