在 Laravel 中,我有一個子類(為了清晰起見,代碼已縮短):
<?php
namespace App\Http\Controllers\Auth;
class RegisterController extends Controller {
...
protected function create(array $data)
{
$twilio = new Client($this->sid, $this->authToken);
$user = $twilio->chat->v2->services(env("TWILIO_CHAT_SERVICE_SID"))
->users
->create($data['username']);
}
...
}
我有父類:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
public $sid;
public $authToken;
public $serviceId;
public function __construct()
{
$this->sid = getenv("TWILIO_ACCOUNT_SID");
$this->authToken = getenv("TWILIO_AUTH_TOKEN", true);
$this->serviceId = getenv("TWILIO_CHAT_SERVICE_SID");
}
}
盡管如此,我還是收到了這個錯誤:
"message": "Argument 1 passed to Twilio\\Rest\\Chat\\V2\\ServiceList::getContext() must be of the type string, null given, called in /home/fingxtbh/thisnthat.com/vendor/twilio/sdk/src/Twilio/Rest/Chat/V2.php on line 80",
"exception": "TypeError",
"file": "/home/fingxtbh/thisnthat.com/vendor/twilio/sdk/src/Twilio/Rest/Chat/V2/ServiceList.php",
"line": 131,
"trace": [
{
"file": "/home/fingxtbh/thisnthat.com/vendor/twilio/sdk/src/Twilio/Rest/Chat/V2.php",
"line": 80,
"function": "getContext",
"class": "Twilio\\Rest\\Chat\\V2\\ServiceList",
"type": "->"
},
{
"file": "/home/fingxtbh/thisnthat.com/app/Http/Controllers/Auth/RegisterController.php",
"line": 104,
為什么不能從父類正確讀取變數 $this->serviceId?
uj5u.com熱心網友回復:
env 呼叫不應該是這樣的嗎?
public function __construct()
{
parent::__construct();
$this->sid = env("TWILIO_ACCOUNT_SID");
$this->authToken = env("TWILIO_AUTH_TOKEN", true);
$this->serviceId = env("TWILIO_CHAT_SERVICE_SID");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/362834.html
上一篇:C#中創建實體的案例
下一篇:函式只顯示一次屬性
