Home > Enterprise >  Laravel parent class property inheritance problem
Laravel parent class property inheritance problem

Time:11-22

In laravel, I have a child class (code shortened for clarity):

<?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']);

    }
    ...
}

And I have the parent class:

<?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");

    }
}

Still, I get this error:

"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,

The Why isn't the variable $this->serviceId being read correctly from the parent class?

CodePudding user response:

Shouldn't the env call should be like this?

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");

}
  • Related