Declare a PHP Trait Property Without Conflicting With the Using Class
Read a property supplied by a PHP trait, then compare visibility, type and default value when the using class declares the same name.
A property declared in a PHP trait becomes part of each class that uses it, so an instance can expose that property. If the class also declares the same name, do not assume the duplicate is automatically an error. For the PHP 8.x rule described by the Generalist Programmer trait guide, the declarations must be compatible in visibility and, where applicable, type and default value. An incompatible pair causes a fatal error.
Declare a property in a trait and read it through the class
HasStatus supplies $status. Entry uses the trait but does not declare its own $status:
<?php
trait HasStatus
{
public $status = 'ready';
}
class Entry
{
use HasStatus;
}
$entry = new Entry();
echo $entry->status;
The expected output is:
ready
The declaration is in HasStatus, while the read is through an Entry instance. The Scientech Easy trait example describes this relationship: trait properties become part of the using class, and its example reads an updated trait property through an object. Here, the public $status property starts with the value 'ready', so the read in this teaching example produces ready without an intervening assignment.
That distinction is useful when inspecting a class. A search limited to property declarations inside Entry would not find the declaration supplying $entry->status. Check the traits named in the class’s use statements as well. The opening <?php makes the snippet a standalone PHP example; the ready block above shows output, not additional PHP code.
Check a same-named declaration in PHP 8.x
Suppose Entry already declares $status when you add HasStatus. Compare the two declarations. In the following separate example, their visibility and default value match. Neither declaration specifies a type:
<?php
trait HasStatus
{
public $status = 'ready';
}
class Entry
{
use HasStatus;
public $status = 'ready';
}
$entry = new Entry();
echo $entry->status;
The expected output is again ready. The repeated declaration is there to illustrate the compatibility check, not to provide a second property with a different value. If you change one declaration to a typed property, check the type on the other declaration too. In the PHP 8.x evidence, same-named trait and class properties must match in visibility and, for typed or initialized properties, in type and default value.
Now compare an incompatible case. This is a separate expected fatal-error example, not code to put alongside either working example:
<?php
trait HasStatus
{
public $status = 'ready';
}
class Entry
{
use HasStatus;
public $status = 'pending';
}
Both declarations are public and untyped, but their default values differ. Under the stated PHP 8.x compatibility rule, that difference makes the declarations incompatible and PHP raises a fatal error. Merely finding the name $status twice does not tell you which example you have. The declarations themselves do.
Handle an incompatible declaration
If a using class has an incompatible $status, decide which declaration should own that name and change the declarations so they no longer conflict. Do not reach for insteadof: the property-conflict rule explicitly says there is no insteadof for properties. For this decision, inspect the trait and class side by side, checking visibility, any declared type, and the applicable default value before treating the shared name as either safe or fatal.
