PHP开发 之 注解

目录

开始

mkdir annotation && cd annotation

mkdir -p module/Base

vim module/Base/Description.php
<?php

namespace Base;

class Description
{
    public $desc = 'default';
}
vim composer.json
{
    "autoload": {
        "psr-4": {
            "Base\\": "module/Base/"
        }
    }
}
composer update

关于命名空间 更多参考PHP学习 之 namespace

vim index.php
<?php

require "vendor/autoload.php";

use Base\Description;

var_dump(new Description());

关于自动加载 更多参考PHP学习 之 autoload

  • 测试
php index.php
object(Base\Description)#3 (1) {
  ["desc"]=>
  string(7) "default"
}

关于PHP环境搭建 更多参考PHP开发 之 开发环境

注解

composer require doctrine/annotations

vim module/Base/Description.php
<?php

namespace Base;

/**
 * @Annotation
 */
class Description
{
    public $desc = 'default';
}

关于doctrine/annotations 更多参考Doctrine Annotations

类注解

vim index.php
<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\AnnotationReader;

$loader = require __DIR__ . '/vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

use Base\Description;

/**
 * @Description(desc="class")
 */
class AnnotationDemo
{

    public function __construct()
    {
        $annotationReader = new AnnotationReader();
        $reflectionClass = new ReflectionClass(get_class($this));

        /**
         * class annotations
         */
        $classAnnotations = $annotationReader->getClassAnnotations($reflectionClass);
        foreach ($classAnnotations as &$classAnnotation)
        {
            var_dump($classAnnotation);
        }
    }
}

$annotationDemo = new AnnotationDemo();

关于PHP反射 更多参考PHP: 反射

  • 测试
php index.php
object(Base\Description)#13 (1) {
  ["desc"]=>
  string(5) "class"
}

属性注解

vim module/Base/Di.php
<?php

namespace Base;

/**
 * @Annotation
 */
class Di
{
    public $class = null;
}
vim index.php
<?php

# 省略了未修改代码

use Base\Description;
use Base\Di;

/**
 * @Description(desc="class")
 */
class AnnotationDemo
{
    /**
     * @Di(class="Base\Description")
     */
    private $desc;


    public function __construct()
    {
        # 省略了未修改代码

        /**
         * property annotations
         */
        foreach ($reflectionClass->getProperties() as &$property)
        {
            $propertyAnnotations = $annotationReader->getPropertyAnnotations($property);
            foreach ($propertyAnnotations as &$propertyAnnotation)
            {
                /**
                 * @var Di $propertyAnnotation
                 */
                $reflectionClass = new ReflectionClass($propertyAnnotation->class);
                $property->setAccessible(true);
                $property->setValue($this, $reflectionClass->newInstance());
            }
        }
    }
}

$annotationDemo = new AnnotationDemo();
var_dump($annotationDemo);

关于ReflectionProperty 更多参考PHP: ReflectionProperty

  • 测试
php index.php
object(AnnotationDemo)#3 (1) {
  ["desc":"AnnotationDemo":private]=>
  object(Base\Description)#9 (1) {
    ["desc"]=>
    string(7) "default"
  }
}

参考

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容