Last active
May 29, 2018 18:51
-
-
Save JoaoGFarias/a4f53ace455823ccca90530d7b8bba06 to your computer and use it in GitHub Desktop.
PHP 7 Anonymous Classes
This file contains hidden or 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 | |
abstract class AbstractClass { // Could be an interface | |
public function concentreMethod() | |
{ | |
echo('I am a concrete method which will not be overwritten'); | |
} | |
public function otherConcentreMethod() | |
{ | |
echo('I am a concrete method which will not be overwritten'); | |
} | |
abstract public function abstractMethod(); | |
} | |
$newObject = new class () extends AbstractClass { | |
public function otherConcentreMethod() // Overriding a concrete method | |
{ | |
echo('I am a concrete method which is being overwritten'); | |
} | |
public function abstractMethod() | |
{ | |
echo('In the concrete anonymous class, I have an implementation'); | |
} | |
} | |
$this->assertInstanceOf( | |
AbstractClass::class, | |
$newObject | |
); // $newObject is an instance of the AbstractClass, although I have not created any named concrete class. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment