Last active
January 25, 2022 13:51
-
-
Save JerryBels/608a7358b0ec8356ab29e9ffb131d617 to your computer and use it in GitHub Desktop.
Anonymous class generation and usage (from PHP 7)
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 | |
interface it { | |
public function tf(); | |
} | |
trait t { | |
public $tv = "tv"; | |
public function tf() { | |
return "tf"; | |
} | |
} | |
class p { | |
public $pv = "pv"; | |
} | |
class a { | |
public function createNewClass(){ | |
$serviceClass = new class() extends p implements it { | |
use t; | |
public $name; | |
public function __construct() | |
{ | |
$this->name = "testService0"; | |
} | |
}; | |
$serviceClassName = get_class($serviceClass); | |
class_alias($serviceClassName, "testlol"); | |
} | |
} | |
class b { | |
public function useNewClass() { | |
$className = "testlol"; | |
$class = new $className; | |
echo $class->name . " - " . $class->pv . " - " . $class->tv. " - " . $class->tf(); | |
} | |
} | |
$a = new a; | |
$b = new b; | |
$a->createNewClass(); | |
$b->useNewClass(); | |
// Returns testService0 - pv - tv - tf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment