简体   繁体   English

trait - 从 trait 获取实现的类

[英]trait - get implemented class from trait

I have following trait implementation:我有以下特征实现:

static function getNextHashId($class)
{
    $data = DB::table("information_schema.TABLES")
        ->where("TABLE_SCHEMA", env('DB_DATABASE'))
        ->where("TABLE_NAME", with(new static)->getTable())
        ->pluck("AUTO_INCREMENT")[0];
    return Hashids::connection($class)->encode($data);
}

But when I have many class implementing the traits, it's not efficient since I need to pass class to the method.但是当我有很多类实现这些特征时,效率不高,因为我需要将类传递给方法。

   Class1::getNextHashId(Class1::class);
   Class2::getNextHashId(Class2::class);

Is it possible to remove get class from traits?是否可以从特征中删除 get 类? so I just need to write Class1::getNextHashId();所以我只需要写Class1::getNextHashId(); How can it be done?怎么做到呢?

进一步搜索后,可以通过静态绑定来完成。

return Hashids::connection(static::class)->encode($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM