Czy konstruktor z traita nie powinien być domyślnie gotowy do użycia w klasie, która ten trait zawiera? W nowszych wersjach PHP jest przecież przeciążanie konstruktorów i innych metod? Mam taki przykładowy, bezużyteczny kod:
<?php
trait Authenticable
{
private $password;
public function __construct($password)
{
$this->password = $password;
}
public function getPassword()
{
return $this->password;
}
}
class Customer {
use Authenticable;
public $id;
public $name;
public $email;
public $balance;
public function __construct($id, $name, $email, $balance)
{
$this->id = $id;
$this->name = $name;
$this->email = $email;
$this->balance = $balance;
}
}
$customer = new Customer('password');
Dostaję błąd
Uncaught ArgumentCountError: Too few arguments to function Customer::__construct(), 1 passed in C:\xampp\htdocs\phpsandbox\oop\index.php on line 34 and exactly 4 expected in C:\xampp\htdocs\phpsandbox\oop\index.php:25 Stack trace: #0 C:\xampp\htdocs\phpsandbox\oop\index.php(34): Customer->__construct('password') #1 {main} thrown in C:\xampp\htdocs\phpsandbox\oop\index.php on line 25
Żeby nie było wątpliwości: wiem jak ten problem "obejść", ale... dlaczego coś takiego nie działa?
EDIT:
Nie ważne, doczytałem trochę i okazało się, że w PHP przeciążanie jest możliwe, ale nie w ten sposób.