samedi 26 décembre 2015

using reflection methods in traits

I have 15 Entity Classes, and I want to put getEntityName static function in there but I don't want to make code duplication, and some of my entities extends vendor related class so I cannot put an abstract class to extend from these entities. I wanted to use traits.

<?php
namespace FrontendBundle\Traits\Entity;

trait GetEntityNameTrait {

    public static function getEntityName()
    {
      return str_replace('\\', '\\\\', get_parent_class());
    }
}

I use this trait in any of this entities, but I'm getting following compile error.

Compile Error: Cannot redeclare class FrontendBundle\Entity\Country

I tried several reflection methods all are the same result is this a bug or something is not logical ?

Others:

get_parent_class()      
get_class()
get_called_class()
__CLASS__





Aucun commentaire:

Enregistrer un commentaire