我可以在 class 中定义 trait 的变量吗?
Can I define trait's variable in class?
当我调用 $article->test()->uri('test-content') 函数时,这是在 uri 变量的特征中显示测试内容,但在文章 class uri 变量 return 为空。如何使用此调用定义 uri 变量?
Traits/Query.php:
trait Query
{
protected $uri;
protected $v = 'v1';
public function uri($uri)
{
$this->uri = $uri;
return $this;
}
Content/Article.php
class Article extends Api
{
use Query;
public $content;
public function __construct() {
parent::__construct();
}
public function test() {
$this->content = $this->v . ' - ' . $this->uri;
return $this;
}
当你做的时候
$article->test()->uri('test-content');
它首先调用 $article->test()
。所以你还没有调用 Query::uri()
来填充 $uri
变量。你需要反转它:
$article->uri('test-content')->test();
当我调用 $article->test()->uri('test-content') 函数时,这是在 uri 变量的特征中显示测试内容,但在文章 class uri 变量 return 为空。如何使用此调用定义 uri 变量?
Traits/Query.php:
trait Query
{
protected $uri;
protected $v = 'v1';
public function uri($uri)
{
$this->uri = $uri;
return $this;
}
Content/Article.php
class Article extends Api
{
use Query;
public $content;
public function __construct() {
parent::__construct();
}
public function test() {
$this->content = $this->v . ' - ' . $this->uri;
return $this;
}
当你做的时候
$article->test()->uri('test-content');
它首先调用 $article->test()
。所以你还没有调用 Query::uri()
来填充 $uri
变量。你需要反转它:
$article->uri('test-content')->test();