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?
My Answer:
php
class ParentClass
{
protected $property = 'parent property';
public function getProperty()
{
return $this->property;
}
}
class ChildClass extends ParentClass
{
protected $property = 'child property';
}
$child = new ChildClass();
echo $child->getProperty(); // Output: 'parent property'
In this example, when we create an instance of `ChildClass` and call the `getProperty()` method, it returns the value of the property from the parent class instead of the child class. This is because the property in the child class is shadowing the property in the parent class, but the `getProperty()` method is still accessing the property from the parent class.
To fix this issue, you can either remove the property declaration in the child class and rely on inheritance, or you can override the `getProperty()` method in the child class to access the child class property:
php
class ChildClass extends ParentClass
{
protected $property = 'child property';
public function getProperty()
{
return $this->property;
}
}
$child = new ChildClass();
echo $child->getProperty(); // Output: 'child property'
By overriding the `getProperty()` method in the child class, we can access the property from the child class instead of the parent class.
Rate this post
4 of 5 based on 5311 votesComments