Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic method can access caller object, bug or feature?
    primarykey
    data
    text
    <p>I was working on my application and discovered strange behaviour of methods that called statically but not defined as static that extends same class. Eventually this methods can access and alter caller protected variables and methods. Here is example of my code:</p> <pre><code>&lt;?php class object { private $version; protected $alteredBy = 'nobody'; public function __construct() { $this-&gt;version = PHP_VERSION; $this-&gt;objectName = get_class($this); echo sprintf("&lt;pre&gt;&lt;strong&gt;New %s Created&lt;/strong&gt;", $this-&gt;objectName); } public function __destruct() { echo sprintf("&lt;/pre&gt;&lt;strong&gt;Source Code&lt;/strong&gt;&lt;div&gt;%s&lt;/div&gt;", 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-&gt;objectName); } } class thirdParty extends object { public function method() { if(is_object($this)) { $this-&gt;alteredBy = __CLASS__; return sprintf( "&lt;span style=\"color:red\"&gt;Object '%s' was altered successfully by %s class&lt;/span&gt;\n", get_class($this), __CLASS__ ); } else return "Cannot access caller object\n\n"; } } print new superApplication; ?&gt; </code></pre> <p>This behaviour is not documented, so I'm wondering is it bug or feature and could it lead to security issues?</p> <p><strong>UPDATE.</strong> I'm aware that $this is not allowed inside static methods and this behaviour appeared on php version 5.2.11</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload