Skip to content

Instantly share code, notes, and snippets.

@azjezz azjezz/attributes.php Secret

Created Feb 17, 2019
Embed
What would you like to do?
Hack-like attributes
<?php declare(strict_types=1);
/**
* Builtin interfaces for attributes :
* - ClassAttribute
* - FunctionAttribute
* - TypeParameterAttribute
* - MethodAttribute
* - StaticPropertyAttribute
* - EnumAttribute
* - InstancePropertyAttribute
* - TypeAliasAttribute
* - PropertyAttribute
* - ClassLikeAttribute
* - FileAttribute
*
* see : https://docs.hhvm.com/hack/attributes/introduction
* see : https://docs.hhvm.com/hack/reference/interface
*/
namespace Doctrine\Orm {
use ClassAttribute;
use PropertyAttribute;
final class Entity implements ClassAttribute {
public string $table;
public function __construct(string $table) {
$this->table = $table;
}
}
final class Column implements PropertyAttribute {}
final class Id implements PropertyAttribute {}
}
namespace App\Entity {
use Doctrine\Orm;
<<Orm\Entity('users')>>
final class User {
<<Orm\Id(), Orm\Column()>>
public int $id;
<<Orm\Column()>>
public string $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}
}
namespace {
$rc = ReflectionClass(App\Entity\User::class);
/**
* @var null|Doctrine\Orm\Entity $entity
*/
$entity = $rc->getAttributeClass(Doctrine\Orm\Entity::class);
if (null === $entity) {
echo 'class doesn\'t have the entity attribute';
} else {
echo $entity->table;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.