-
-
Save azjezz/918e333646d4f4070ab0be4cb41a81de to your computer and use it in GitHub Desktop.
Hack-like attributes
This file contains 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 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