Skip to content

Instantly share code, notes, and snippets.

@azjezz
Created February 17, 2019 00:11
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 azjezz/918e333646d4f4070ab0be4cb41a81de to your computer and use it in GitHub Desktop.
Save azjezz/918e333646d4f4070ab0be4cb41a81de to your computer and use it in GitHub Desktop.
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