drdawt9210 2019-08-07 13:03
浏览 35

在特征中查找类属性,但只查找类的属性

I am trying to isolate the attributes of an originating class in an included trait. IE The trait should make an array of the names of all the attributes of the class but not the attributes of the trait for use within the trait.

I have tried doing this by extending a class. I have tried using static methods as per PHP: Is it possible to get the name of the class using the trait from within a trait static method? and I am getting nowhere.

I am about to use known attributes in the trait and simply remove them from the attribute array (as I know their names). This is a rather ugly solution but it will work.

Anyone see a better way to do this?

trait FooTrait
{
  public $classVariables;

  public function classAttributes()
  {
    $callingClass = get_class($this);
    $rawAttributes= $this->$classVariables = get_class_vars($callingClass);
    var_dump($rawAttributes);
    var_dump($callingClass);
    return $rawAttributes;
  }

  public function info()
  {
    var_dump($this->classVariables);
  }

  // manipulate $this -> classVaribales to do generic database operations

}

class Mine
{
  use FooTrait;
  protected $attrib1;
  protected $attrib2;
  protected $attrib3;
}

$needed = new Mine;

$needed->classAttributes();
$needed->info();

OUTPUT is attribute 1,2,3 and bar. How do I get just attribute 1, 2, 3?

EDIT: I edited a couple of attributes to try and make it more comprehensible.

  • 写回答

1条回答 默认 最新

  • dshnx48866 2019-08-08 15:08
    关注

    UPDATE: This does not work if trait attributes are protected or private. As traits should not be directly referenced ... bit of a deal breaker.

    The only way I could find to get the attributes of the trait WITHOUT those of the calling class was to name it as a literal. BUT that limits the scope and so cannot see the private and protected attributes.

    I am giving up at this point and will use an array of the names of attributes used in the trait. Not a big problem just massively inelegant.

    class ThisClass {
    use ThisTrait;
    public $classAttribute1 = 3;
    public $classAttribute2 = 3;
    public $classAttribute3 = 3;
    }
    
    trait ThisTrait {
      public $traitTrait1 = 3;
      public $traitTrait2 = 3;
      public $traitTrait3 = 3;
    
      public function classAttributes (){
        $traitAttributes = get_class_vars("ThisTrait"); //NB String not variable
        $traitAttributes = array_keys ($traitAttributes);
        $className = get_class($this); //NB Var = gets class where this called
        $classAttributes = get_class_vars($className);
        $classAttributes = array_keys($classAttributes);
    
        $classOnly = array_diff($classAttributes, $traitAttributes);
        return $classOnly;
      }
    }
    
    $thisClass = new ThisClass ();
    $result = $thisClass -> classAttributes();
    var_dump($result);
    
    =========================================
    array (size=3)
    0 => string 'classAttribute1' (length=15)
    1 => string 'classAttribute2' (length=15)
    2 => string 'classAttribute3' (length=15)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的