附录:无法使用ReflectionAnnotatedProperty类从属性获取注释

时间:2015-03-20 16:20:01

标签: php annotations

我正在尝试使用addendum ReflectionAnnotatedProperty从我的系统类中获取名称属性注释,我得到了以下异常:

*exception 'ReflectionException' with message 'Property System::$name does not exist'*

以下是调用ReflectionAnnotatedProperty类的方法:

<?php

namespace cfg\app\db;

require 'vendor/addendum/Annotations.php';
require 'cfg/app/db/Table.php';

/**
 * do not take care about french comments ;)
 * Classe Magique crééé pour faire des requêtes dans des
 * tables de la base de données et retourner des resultats.<br />
 * Dans cette version 1, elle supporte seulement le SELECT statement
 *
 * @author Kiala
 * @name Magic NtoProg Query
 * @version 1.0.0
 */
class MagicNtpQuery extends Connector implements DBInterface {

    const TYPE_ONE = 1;
    const TYPE_ALL = 2;
    const OPERATION_AND = 1;
    const OPERATION_OR = 2;

    private $operations = array(
        self::OPERATION_AND => "AND",
        self::OPERATION_OR => "OR"
    );

    /**
     * retourne le nom de la classe actuelle
     * returns the actual class name
     * 
     * @return string
     *
     * Here everything work!
     */
    private function getClassName() {

        $obj = new \ReflectionObject($this);
        
        $name = substr($obj->getName(), strrpos($obj->getName(), "\\") + 1);
        
        $tableManager = explode('Manager', $name);
        
        $cls = new \ReflectionAnnotatedClass('models\\'. $tableManager[0]);
        
        if ($cls->hasAnnotation('Table')) {
            return $cls->getAnnotation('Table')->value;
        }
        
        return $tableManager[0];
    }
    
    /**
     *
     * return the actual class using ReflectionObject. working!
     */
    private function getClass() {

        $obj = new \ReflectionObject($this);
        
        $name = substr($obj->getName(), strrpos($obj->getName(), "\\") + 1);
        
        $tableManager   = explode('Manager', $name);
        $classname      = "models\\" . $tableManager[0];
        
        $class = new \ReflectionAnnotatedClass($classname);
        
        return $class;
    }
    
    /**
     * the problem is here.
     */
    public function persist($object) {
        
        if (is_object($object)) {
            
            $class = $this->getClass();
            
            if ($class->hasAnnotation('Table')) {
                
                //return $class->getAnnotation('Table')->value;
                
                // don't work
                $properties = new \ReflectionAnnotatedProperty($this->getClassName(), 'name');
                
                $annotations = $properties->getAllAnnotations();
            }
        }
    }

}

这是我的System类:

<?php

namespace models;

/**
 * Description of System
 *
 * @author Kiala
 * @Table("system")
 */
class System {
    
    /**
     * @Column(name="id", type="integer", primary=true)
     *
     * @var int 
     */
    private $id;
    
    /**
     * @Column(name="name", type="string", length=255)
     *
     * @var string 
     */
    private $name;
    
    /**
     * @Column(name="system_type_id", type="integer")
     * @Relation(target="\models\SystemType", column="id")
     *
     * @var type 
     */
    private $system_type;

    //...
}

请帮助。

0 个答案:

没有答案