function a(){ // We shall remove the following solution when publish as KATA static $poi = null; if ($poi) return $poi->create(); $poi = new class { public function create() { return new static(); } }; return $poi->create(); }
- function a(){
- // We shall remove the following solution when publish as KATA
- static $poi = null;
- if ($poi) return $poi->create();
- $poi = new class {
- public function create() {
- return new static();
- }
- };
- return $poi->create();
- }
// PHPUnit Test Examples: // TODO: Replace examples and use TDD development by writing your own tests class MyTestCases extends TestCase { // test function names should start with "test" public function testSameClassName() { $this->assertSame(get_class(a()), get_class(a())); } public function testAnonymous() { $this->assertSame(0, strpos(get_class(a()), "class@anonymous")); } public function testDifferentIns() { $this->assertSame(a() !== a(), true); } }
- // PHPUnit Test Examples:
- // TODO: Replace examples and use TDD development by writing your own tests
- class MyTestCases extends TestCase
- {
- // test function names should start with "test"
public function testThatSomethingShouldHappen() {- public function testSameClassName() {
- $this->assertSame(get_class(a()), get_class(a()));
- }
- public function testAnonymous() {
- $this->assertSame(0, strpos(get_class(a()), "class@anonymous"));
- }
- public function testDifferentIns() {
- $this->assertSame(a() !== a(), true);
- }
- }
This is a solution
of creating multiple instance from the same anoynoumous class.
function a(){ static $poi = null; if ($poi) return $poi->create(); $poi = new class { public function create() { return new static(); } }; return $poi->create(); }
function a(){return new class {};}$twin1 = a();$twin2 = a();- function a(){
- static $poi = null;
- if ($poi) return $poi->create();
- $poi = new class {
- public function create() {
- return new static();
- }
- };
- return $poi->create();
- }
// PHPUnit Test Examples: // TODO: Replace examples and use TDD development by writing your own tests class MyTestCases extends TestCase { // test function names should start with "test" public function testThatSomethingShouldHappen() { $this->assertSame(get_class(a()), get_class(a())); } }
- // PHPUnit Test Examples:
- // TODO: Replace examples and use TDD development by writing your own tests
- class MyTestCases extends TestCase
- {
- // test function names should start with "test"
- public function testThatSomethingShouldHappen() {
- $this->assertSame(get_class(a()), get_class(a()));
- }
- }