php8新特性注解
2022年3月9日09:21:06
之前就有人利用phpdoc实现了注解,但是性能一般,现在php8也追加了注解,现在8.1的fiber也加入了,未来PHP异步编程也趋于完善,剩下就是泛型的加入了,就fb的hacklang类似的语言
原理和java的注解基本一致,使用起来比java简单
基本使用就是在类或者方法上挂载注解,在注解里面注入一些参数,然后使用反射类,去反射被挂在注解的类或者方法的参数,在注解类里面去执行一些方法,一般都会配合aop编程在实现自动执行
aop就是自动扫描挂在注解的方法所在的文件或者文件路径
比如在controller上挂在路由,估计PHP注解开始大面积使用的时候,对应的aop包也会一起出现
基本使用
注解类 注解类也是可以继承的
#[Attribute(Attribute::TARGET_ALL)] class father { private string $path; private string $method; public function __construct(string $path, string $method = 'GET') { $this->path = $path; $this->method = $method; } public function getPath() { return $this->path; } public function getMethod() { return $this->method; } }
在类上面挂载注解
<?php include_once './zx.php'; #[father('/data', 'POST')] class tt { #[father('/post', 'POST')] public function postName() { } #[father('/get', 'GET')] public function getName() { } }
通过aop扫描所有使用注解的类,其实就是psr4标准,类的空间命名,去扫描对应的目录,吧文件的所有类通过反射获取对应值,然后执行某些操作,aop类就不写了,直接写反射的具体类容
$rc = new ReflectionClass(tt::class); $r_atts = $rc->getAttributes(father::class); //因为可能出现 IS_REPEATABLE 默认不能重复挂载注解,加上这个就是可以重复挂在 //print_r($r_atts['0']->getName()); $f_path = $r_atts['0']->getName(); print_r($f_path); print_r(PHP_EOL); print_r($r_atts['0']->newInstance()->getPath()); print_r(PHP_EOL); //获取子目录的 $rr = $rc->getMethods(); foreach ($rr as $k => $v) { print_r($v->getName()); print_r(PHP_EOL); foreach ($v->getAttributes() as $kk => $vv) { print_r($vv->getName()); print_r(PHP_EOL); // print_r($vv->getArguments()); print_r($vv->newInstance()->getPath()); print_r(PHP_EOL); } }
PS D:\phpstudy_pro\WWW\www.kk.com> php80 .\index.php father /data postName father /post getName father /get
这样就是吧path拼接完整的path,写入到路由表里
php原始的属性注解
#[Attribute(Attribute::TARGET_CLASS)] final class Attribute { public int $flags; /** * 标记仅在类中允许属性声明 */ public const TARGET_CLASS = 1; /** * 标记仅在函数中允许属性声明 */ public const TARGET_FUNCTION = 2; /** * 标记仅在类方法中允许属性声明 */ public const TARGET_METHOD = 4; /** * 标记仅在类属性中允许属性声明 */ public const TARGET_PROPERTY = 8; /** * 标记仅在类常量中允许属性声明 */ public const TARGET_CLASS_CONSTANT = 16; /** * 标记仅在函数或方法参数中允许属性声明 */ public const TARGET_PARAMETER = 32; /** * 标记任何地方都允许属性声明 */ public const TARGET_ALL = 63; /** * 注意同一个地方的属性声明是,默认 否 * 允许多次。 */ public const IS_REPEATABLE = 64; /** * @param int $flags 位掩码形式的值,指示位置 * 可以定义属性的地方。 */ public function __construct(#[ExpectedValues(flagsFromClass: Attribute::class)] int $flags = self::TARGET_ALL) { } }
<?php /** * The ReflectionClass class reports information about a class. * * @link https://php.net/manual/en/class.reflectionclass.php */ class ReflectionClass implements Reflector { /** * @var string 类名,与调用 {@see ReflectionClass::getName()} 方法相同 */ #[Immutable] public $name; /** * 表示类是抽象的,因为它有一些抽象方法。 * * @link https://www.php.net/manual/en/class.reflectionclass.php#reflectionclass.constants.is-implicit-abstract */ public const IS_IMPLICIT_ABSTRACT = 16; /** * 表示由于其定义而抽象的类。 * * @link https://www.php.net/manual/en/class.reflectionclass.php#reflectionclass.constants.is-explicit-abstract */ public const IS_EXPLICIT_ABSTRACT = 64; /** * 表示 final class. 表示是最终不能修改的类 * * @link https://www.php.net/manual/en/class.reflectionclass.php#reflectionclass.constants.is-final */ public const IS_FINAL = 32; /** * 构造一个反射类 * * @link https://php.net/manual/en/reflectionclass.construct.php * @param string|object $objectOrClass 包含名称的字符串 * 要反映的类或对象 * @throws \ReflectionException 如果类不存在。 */ public function __construct($objectOrClass) {} /** * 导出一个反射类 * * @link https://php.net/manual/en/reflectionclass.export.php * @param mixed $argument 要导出的反射。 * @param bool $return 设置为 {@see true} 将返回导出,如 * 反对发射它。设置为 {@see false}(默认值)将起到相反的作用。 * @return string|null 如果 $return 参数设置为 {@see true},则 * export 作为字符串返回,否则返回 {@see null}。 * @removed 8.0 */ #[Deprecated(since: '7.4')] public static function export($argument, $return = false) {} /** * 返回 ReflectionClass 对象的字符串表示形式。 * * @link https://php.net/manual/en/reflectionclass.tostring.php * @return string 此 {@see ReflectionClass} 实例的字符串表示形式。 */ public function __toString() {} /** * 获取类名 * * @link https://php.net/manual/en/reflectionclass.getname.php * @return string 类名。 */ #[Pure] public function getName() {} /** * 检查类是由扩展内部定义的,还是由核心定义的 * * @link https://php.net/manual/en/reflectionclass.isinternal.php * @return bool 成功返回 {@see true},失败返回 {@see false}。 */ #[Pure] public function isInternal() {} /** * 检查用户是否定义 * * @link https://php.net/manual/en/reflectionclass.isuserdefined.php * @return bool 成功返回 {@see true},失败返回 {@see false}。 */ #[Pure] public function isUserDefined() {} /** * 检查类是否可实例化 * * @link https://php.net/manual/en/reflectionclass.isinstantiable.php * @return bool Returns {@see true} on success or {@see false} on failure. */ #[Pure] public function isInstantiable() {} /** * 返回此类是否可克隆 * * @link https://php.net/manual/en/reflectionclass.iscloneable.php * @return bool Returns {@see true} if the class is cloneable, {@see false} otherwise. * @since 5.4 */ #[Pure] public function isCloneable() {} /** * 获取已定义类的文件的文件名 * * @link https://php.net/manual/en/reflectionclass.getfilename.php * @return string|false the filename of the file in which the class has been defined. * If the class is defined in the PHP core or in a PHP extension, {@see false} * is returned. */ #[Pure] public function getFileName() {} /** * 获取起始行号 * * @link https://php.net/manual/en/reflectionclass.getstartline.php * @return int The starting line number, as an integer. */ #[Pure] public function getStartLine() {} /** * 获取结束行 * * @link https://php.net/manual/en/reflectionclass.getendline.php * @return int|false The ending line number of the user defined class, or * {@see false} if unknown. */ #[Pure] public function getEndLine() {} /** * 获取文档评论 * * @link https://php.net/manual/en/reflectionclass.getdoccomment.php * @return string|false The doc comment if it exists, otherwise {@see false} */ #[Pure] public function getDocComment() {} /** * 获取类的构造函数 * * @link https://php.net/manual/en/reflectionclass.getconstructor.php * @return ReflectionMethod|null A {@see ReflectionMethod} object reflecting * the class' constructor, or {@see null} if the class has no constructor. */ #[Pure] public function getConstructor() {} /** * 检查方法是否定义 * * @link https://php.net/manual/en/reflectionclass.hasmethod.php * @param string $name Name of the method being checked for. * @return bool Returns {@see true} if it has the method, otherwise {@see false} */ public function hasMethod($name) {} /** * 获取类方法的 ReflectionMethod。 * * @link https://php.net/manual/en/reflectionclass.getmethod.php * @param string $name 要反映的方法名称。 * @return ReflectionMethod A {@see ReflectionMethod} * @throws \ReflectionException 如果方法不存在。 */ #[Pure] public function getMethod($name) {} /** * 获取类的方法数组。 * * @link https://php.net/manual/en/reflectionclass.getmethods.php * @param int|null $filter 过滤结果以仅包含方法 * 具有某些属性。默认为无过滤。 * @return ReflectionMethod[] {@see ReflectionMethod} 对象数组 * 反映每种方法。 */ #[Pure] public function getMethods($filter = null) {} /** * 检查是否定义了属性 * * @link https://php.net/manual/en/reflectionclass.hasproperty.php * @param string $name 正在检查的属性的名称。 * @return bool 如果有属性则返回 {@see true},否则返回 {@see false} */ public function hasProperty($name) {} /** * 获取类属性的 ReflectionProperty * * @link https://php.net/manual/en/reflectionclass.getproperty.php * @param string $name 属性名称。 * @return ReflectionProperty A {@see ReflectionProperty} * @throws ReflectionException 如果该名称不存在任何属性。 */ #[Pure] public function getProperty($name) {} /** * 获取属性 * * @link https://php.net/manual/en/reflectionclass.getproperties.php * @param int|null $filter 可选过滤器,用于过滤所需 * 属性类型。它是使用 {@see ReflectionProperty} 常量配置的, * 并且默认为所有属性类型。 * @return 反射属性[] */ #[Pure] public function getProperties($filter = null) {} /** * 获取类属性的 ReflectionClassConstant * * @link https://php.net/manual/en/reflectionclass.getreflectionconstant.php * @param string $name 类常量名。 * @return ReflectionClassConstant A {@see ReflectionClassConstant}. * @since 7.1 */ #[Pure] public function getReflectionConstant(string $name) {} /** * 获取类常量 * * @link https://php.net/manual/en/reflectionclass.getreflectionconstants.php * @param int|null $filter [可选] 允许通过可见性过滤类中定义的常量。从 8.0. * @return ReflectionClassConstant[] ReflectionClassConstant 对象数组。 * @从 7.1 开始 */ #[Pure] public function getReflectionConstants(?int $filter = ReflectionClassConstant::IS_PUBLIC|ReflectionClassConstant::IS_PROTECTED|ReflectionClassConstant::IS_PRIVATE) {} /** * 检查是否定义了常量 * * @link https://php.net/manual/en/reflectionclass.hasconstant.php * @param string $name 要检查的常量的名称。 * @return bool 如果定义了常量,则返回 {@see true},否则返回 {@see false} */ public function hasConstant($name) {} /** * 获取常量 * * @link https://php.net/manual/en/reflectionclass.getconstants.php * @param int $filter [可选] 允许通过可见性过滤类中定义的常量。从 8.0. * @return array 一个常量数组,其中键保存名称和 * values 常量的值。 */ #[Pure] public function getConstants($filter = ReflectionClassConstant::IS_PUBLIC|ReflectionClassConstant::IS_PROTECTED|ReflectionClassConstant::IS_PRIVATE) {} /** * 获取定义的常量 * * @link https://php.net/manual/en/reflectionclass.getconstant.php * @param string $name 常量的名称。 * @return mixed|false 名称为 name 的常量的值。 * 如果在类中找不到常量,则返回 {@see false}。 */ #[Pure] public function getConstant($name) {} /** * 获取接口 * * @link https://php.net/manual/en/reflectionclass.getinterfaces.php * @return ReflectionClass[] 接口的关联数组,以键为接口 * 名称和数组值作为 {@see ReflectionClass} 对象。 */ #[Pure] public function getInterfaces() {} /** * 获取接口名称 * * @link https://php.net/manual/en/reflectionclass.getinterfacenames.php * @return string[] 以接口名称为值的数值数组。 */ #[Pure] public function getInterfaceNames() {} /** * 检查班级是否匿名 * * @link https://php.net/manual/en/reflectionclass.isanonymous.php * @return bool 成功返回 {@see true},失败返回 {@see false}。 * @从 7.0 开始 */ #[Pure] public function isAnonymous() {} /** * 检查类是否为接口 * * @link https://php.net/manual/en/reflectionclass.isinterface.php * @return bool 成功返回 {@see true},失败返回 {@see false}。 */ #[Pure] public function isInterface() {} /** * 返回该类使用的特征数组 * * @link https://php.net/manual/en/reflectionclass.gettraits.php * @return ReflectionClass[]|null 一个带有特征名称的数组,键和 * 值中 trait 的 {@see ReflectionClass} 的实例。 * 如果发生错误,则返回 {@see null}。 * @从 5.4 开始 */ #[Pure] public function getTraits() {} /** * 返回该类使用的特征名称数组 * * @link https://php.net/manual/en/reflectionclass.gettraitnames.php * @return string[] 值中包含特征名称的数组。 * 如果发生错误,则返回 {@see null}。 * @从 5.4 开始 */ #[Pure] public function getTraitNames() {} /** * 返回一个特征别名数组 * * @link https://php.net/manual/en/reflectionclass.gettraitaliases.php * @return string[] 一个数组,其中键名和原始方法名都有新的方法名 * 值中的名称(格式为“TraitName::original”)。 * 如果发生错误,则返回 {@see null}。 * @从 5.4 开始 */ #[Pure] public function getTraitAliases() {} /** * 返回是否为Trait * * @link https://php.net/manual/en/reflectionclass.istrait.php * @return bool 如果这是一个特征,则返回 {@see true},否则返回 {@see false}。 * 如果发生错误,则返回 {@see null}。 * @从 5.4 开始 */ #[Pure] public function isTrait() {} /** * 检查类是否抽象 * * @link https://php.net/manual/en/reflectionclass.isabstract.php * @return bool Returns {@see true} on success or {@see false} on failure. */ #[Pure] public function isAbstract() {} /** * 检查类是否是最终的 * * @link https://php.net/manual/en/reflectionclass.isfinal.php * @return bool Returns {@see true} on success or {@see false} on failure. */ #[Pure] public function isFinal() {} /** * 获取修饰符 * * @link https://php.net/manual/en/reflectionclass.getmodifiers.php * @return int bitmask of modifier constants. */ #[Pure] public function getModifiers() {} /** * 例如检查类 * * @link https://php.net/manual/en/reflectionclass.isinstance.php * @param object $object 被比较的对象。 * @return bool 成功返回 {@see true},失败返回 {@see false}。 */ #[Pure] public function isInstance($object) {} /** * 从给定的参数创建一个新的类实例。 * * @link https://php.net/manual/en/reflectionclass.newinstance.php * @param mixed ...$args 接受可变数量的参数,这些参数是 * 传递给类构造函数,很像 {@see call_user_func} * @return object 一个新的类实例。 * @throws ReflectionException 如果类构造函数不是公共的,或者如果 * 该类没有构造函数并且 $args 参数包含 * 一个或多个参数。 */ public function newInstance(...$args) {} /** * 创建一个新的类实例而不调用构造函数。 * * @link https://php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php * @return object 一个新的类实例。 * @throws ReflectionException 如果该类是一个内部类 * 不能在不调用构造函数的情况下实例化。在 PHP 5.6.0 中 * 之后,此例外仅限于最终的内部类。 * @从 5.4 开始 */ public function newInstanceWithoutConstructor() {} /** * 从给定的参数创建一个新的类实例。 * * @link https://php.net/manual/en/reflectionclass.newinstanceargs.php * @param array $args 要作为数组传递给类构造函数的参数。 * @return object 一个新的类实例。 * @throws ReflectionException 如果类构造函数不是公共的,或者如果 * 该类没有构造函数并且 $args 参数包含 * 一个或多个参数。 * @since 5.1.3 */ public function newInstanceArgs(array $args = []) {} /** * 获取父类 * * @link https://php.net/manual/en/reflectionclass.getparentclass.php * @return ReflectionClass|false A {@see ReflectionClass} or {@see false} * if there's no parent. */ #[Pure] public function getParentClass() {} /** * 检查是否是子类 * * @link https://php.net/manual/en/reflectionclass.issubclassof.php * @param string|ReflectionClass $class 类的名称为 * 字符串或要检查的类的 {@see ReflectionClass} 对象。 * @return bool {@see true} 成功或 {@see false} 失败 */ #[Pure] public function isSubclassOf($class) {} /** * 获取静态属性 * * @link https://php.net/manual/en/reflectionclass.getstaticproperties.php * @return mixed[] 静态属性,作为键所在的数组 * 名称和值属性的值。 */ #[Pure] public function getStaticProperties() {} /** * 获取静态属性值 * * @link https://php.net/manual/en/reflectionclass.getstaticpropertyvalue.php * @param string $name 要为其返回值的静态属性的名称。 * @param mixed $default 类返回的默认值 * 不声明具有给定名称的静态属性。如果物业有 * 不存在且省略此参数,则抛出 {@see ReflectionException}。 * @return mixed 静态属性的值。 */ #[Pure] public function getStaticPropertyValue($name, $default = null) {} /** * 设置静态属性值 * * @link https://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php * @param string $name 属性名称。 * @param mixed $value 新属性值。 * @return void 不返回任何值。 */ public function setStaticPropertyValue($name, $value) {} /** * 获取默认属性 * * @link https://php.net/manual/en/reflectionclass.getdefaultproperties.php * @return mixed[] 默认属性数组,键为名称 * 的属性和值是属性的默认值 * 或 {@see null} 如果属性没有默认值。功能 * 不区分静态和非静态属性 * 不考虑可见性修饰符。 */ #[Pure] public function getDefaultProperties() {} /** * {@see ReflectionClass::isIterable} 方法的别名。 * * @link https://php.net/manual/en/reflectionclass.isiterateable.php * @return bool 成功返回 {@see true},失败返回 {@see false}。 */ #[Pure] public function isIterateable() {} /** * 检查这个类是否可迭代 * * @link https://php.net/manual/en/reflectionclass.isiterable.php * @return bool 成功返回 {@see true},失败返回 {@see false}。 * @从 7.2 开始 */ #[Pure] public function isIterable() {} /** * 检查它是否实现了一个接口。 * * @link https://php.net/manual/en/reflectionclass.implementsinterface.php * @param string $interface The interface name. * @return bool Returns {@see true} on success or {@see false} on failure. */ public function implementsInterface($interface) {} /** * 为定义类的扩展获取一个 ReflectionExtension 对象 * * @link https://php.net/manual/en/reflectionclass.getextension.php * @return ReflectionExtension 一个 {@see ReflectionExtension} 对象表示 * 定义类的扩展名,或者 {@see null} 用于用户定义的类。 */ #[Pure] public function getExtension() {} /** * 获取定义类的扩展名 * * @link https://php.net/manual/en/reflectionclass.getextensionname.php * @return string|false 定义类的扩展名, * 或 {@see false} 用于用户定义的类。 */ #[Pure] public function getExtensionName() {} /** * 检查是否在命名空间 * * @link https://php.net/manual/en/reflectionclass.innamespace.php * @return bool {@see true} on success or {@see false} on failure. */ public function inNamespace() {} /** * 获取命名空间名称 * * @link https://php.net/manual/en/reflectionclass.getnamespacename.php * @return string The namespace name. */ #[Pure] public function getNamespaceName() {} /** * 获取简称 * * @link https://php.net/manual/en/reflectionclass.getshortname.php * @return string The class short name. */ #[Pure] public function getShortName() {} /** * 返回注解的数组 * * @param string|null $name Name of an attribute class * @param int $flags Сriteria by which the attribute is searched. * @return ReflectionAttribute[] * @since 8.0 */ #[Pure] public function getAttributes(?string $name = null, int $flags = 0) {} /** * 克隆对象 * * @link https://php.net/manual/en/reflectionclass.clone.php * @return void */ final private function __clone() {} }