9
I am implementing a filter functionality in some classes of my application through traits.
The function of trait will refer to 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 the trait the property is defined in the class.
I thought to implement this functionality from heritage, but I would lose the flexibility of using the trait in other parts of my application.
Is there any way to "force" the declaration of the properties from the trait? If not, there is some alternative without inheritance?
My problem is not declaring property on trait, this was my first attempt, but if the class has the same property, there will be a collision error with the name of the properties.
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: Stockpile and Filtertrait defines the same Property ($allowedFilters) in the Composition of Estoquemeta. However, the Definition differs and is considered incompatible. Class was composed in [...][...] on line 23
Declare the property on trait does not solve? I do not know the implementation of trait in PHP, why would you lose the ability to use in other classes?
– Maniero
@bigown already tried, but php does not allow.
– gmsantos
The property needs to be Static? you can declare properties in Trait without any problem!
– Trxplz0
http://meta.pt.stackoverflow.com/questions/1901/essa-pergunta-pode-ser-considerada-uma-pergunta-camale%C3%A3o
– gmsantos