'Why do I get "definition differs" if they are the same?
In Laravel, I have a class User that extends Authenticatable and uses a trait called Activable:
class User extends Authenticable {
use Activable;
protected $is_active_column = 'is_active';
}
trait Activable {
/**
* The name of the column with the activable status
*
* @var string
*/
protected $is_active_column = 'is_active';
}
When I try to find a User in tinker I get this error:
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
PHP Fatal error: App\Models\User and App\Traits\Activable define the same property ($is_active_column) in the composition of App\Models\User. However, the definition differs and is considered incompatible.
Solution 1:[1]
This is intended behavior. The PHP documentation on Traits is explicit about this
Properties
Traits can also define properties.
Example #12 Defining Properties
<?php trait PropertiesTrait { public $x = 1; } class PropertiesExample { use PropertiesTrait; } $example = new PropertiesExample; $example->x; ?>If a trait defines a property then a class can not define a property with the same name unless it is compatible (same visibility and initial value), otherwise a fatal error is issued.
Example #13 Conflict Resolution
<?php trait PropertiesTrait { public $same = true; public $different = false; } class PropertiesExample { use PropertiesTrait; public $same = true; public $different = true; // Fatal error } ?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | IGP |
