Created
August 22, 2018 23:20
-
-
Save bulton-fr/eedbf7b3656fe2626cab034cd70319de to your computer and use it in GitHub Desktop.
When we re-instantiate an anonymous class with help of get_called_class function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo | |
{ | |
public function bar() | |
{ | |
$class = get_called_class(); | |
var_dump($class); | |
return new $class('bar'); | |
} | |
} | |
$foo = new class('foo') extends Foo { | |
protected $test; | |
public function __construct($test) { | |
$this->test = $test; | |
} | |
}; | |
echo '<pre>'; | |
var_dump($foo); | |
var_dump($foo->bar()); | |
/* | |
* Tested with http://phptester.net/ in PHP 7.0 | |
object(class@anonymous)#1 (1) { | |
["test":protected]=> | |
string(3) "foo" | |
} | |
string(86) "class@anonymous/home4/phptest/public_html/code.php70(5) : eval()'d code0x7fdfa3a6a4d8" | |
object(class@anonymous)#2 (1) { | |
["test":protected]=> | |
string(3) "bar" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment