Skip to content

Instantly share code, notes, and snippets.

@JerryBels
Last active January 25, 2022 13:51
Show Gist options
  • Save JerryBels/608a7358b0ec8356ab29e9ffb131d617 to your computer and use it in GitHub Desktop.
Save JerryBels/608a7358b0ec8356ab29e9ffb131d617 to your computer and use it in GitHub Desktop.
Anonymous class generation and usage (from PHP 7)
<?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