Skip to content

Instantly share code, notes, and snippets.

@JoaoGFarias
Last active May 29, 2018 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoaoGFarias/a4f53ace455823ccca90530d7b8bba06 to your computer and use it in GitHub Desktop.
Save JoaoGFarias/a4f53ace455823ccca90530d7b8bba06 to your computer and use it in GitHub Desktop.
PHP 7 Anonymous Classes
<?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