我在构造函数中设置一个属性值,如:class ApiController extends Controller { protected $start; protected $limit; protected $string; public function __construct(Request $request) { $this->request = $request; $this->string = 'from: '.$this->start. ', size: '.$this->limit; }并尝试$this->string从类中的方法访问,例如: public function test() { $this->start = 0; $this->limit = 10; echo $this->string; }这与以下输出相呼应:from: , size: 我如何设置$start和$limit属性,因为我正在尝试做。
2 回答
大话西游666
TA贡献1817条经验 获得超14个赞
在构造函数中,当 $this-string 被初始化时,$this->size 和 $this->limit 仍然没有任何值。您必须在初始化 $this->string 之前设置它们的值,或者在 $this->size 和 $this->limit 具有值之后初始化 $this->string 。
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
首先调用您的构造函数,其中您的“字符串”变量(顺便说一句,我建议使用不同的名称)设置为“from: , size:”。
然后在您的 test() 方法中,您设置 start 和 limit 变量,但您回显在您的构造中设置的字符串变量。您的代码尚未使用新值更新“字符串”变量!
简单的解决方案,将您的 $this -> string = ... 行移动/复制到您的 echo $this -> 字符串行上方。
我希望这是有道理的 :)
- 2 回答
- 0 关注
- 223 浏览
添加回答
举报
0/150
提交
取消