Skip to content

Instantly share code, notes, and snippets.

@tacoberu
Last active October 19, 2019 20:49
Show Gist options
  • Save tacoberu/348fe2868989d6d4d4a799c46ae1ede8 to your computer and use it in GitHub Desktop.
Save tacoberu/348fe2868989d6d4d4a799c46ae1ede8 to your computer and use it in GitHub Desktop.
First-class class in the PHP
<?php
$inst = new class("> ") {
private $prefix;
function __construct(string $prefix)
{
$this->prefix = $prefix;
}
function log($msg)
{
echo $this->prefix . $msg;
}
}
dump($inst);
<?php
$klass = class {
private $prefix;
function __construct(string $prefix)
{
$this->prefix = $prefix;
}
function log($msg)
{
echo $this->prefix . $msg;
}
}
dump(new $klass("> "));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment