I'm implementing a filtering feature in some classes of my application through traits
.
The function of trait
will use class variables through some properties defined in the class:
<?php
trait FilterTrait {
public function scopeApplyFilters($filters) {
foreach (self::$allowedFilters as $filter) {
// Executa método
}
}
}
class EstoqueMeta extends Eloquent {
use FilterTrait;
static public $allowedFilters = array('foo','bar');
}
I would like to force from trait
that the property be defined in the class.
I thought I'd implement this functionality from inheritance, but I'd lose the flexibility of using trait
in other parts of my application.
Is there any way to "force" declaration of properties from trait
? If not, is there an alternative without involving inheritance?
My problem is not to declare the property in trait
, even that was my first attempt, but if the class has the same property, a collision error occurs with the properties name.
trait FilterTrait {
public $allowedFilters = array();
public function scopeApplyFilters($filters) {
foreach (self::$allowedFilters as $filter) {
// Executa método
}
}
}
class EstoqueMeta {
use FilterTrait;
public $allowedFilters = array('foo', 'bar');
}
Fatal error: EstoqueMeta and FilterTrait define the same property ($ allowedFilters) in the composition of EstoqueMeta. However, the definition differs and is considered incompatible. Class was composed in [...] on line 23