0
I have 3 classes that have the same function, however, some functions have the content of the implementation different from the others: Ex:
class CategoryServices {
/**
* @var CategoryRepository
*/
protected $repository;
/**
* @var CategoryRepository
*/
protected $validator;
/**
* OwnersController constructor.
*
* @param CategoryRepository $repository
* @param CategoryRepository $validator
*/
public function __construct(CategoryRepository $repository, CategoryValidator $validator)
{
$this->repository = $repository;
$this->validator = $validator;
}
/**
* Recuperar todos os registros ativos
*
* @return array $array
*/
public function all() {
try {
$data = $this->repository->findWhere([ 'status' => 1 ])->all();
// retorna resultado da gravacao
return [
'success' => true,
'message' => null,
'total' => count($data),
'data' => $data,
];
} catch (ValidatorException $e) {
// retorna msg de erro
return [
'success' => false,
'message' => $e->getMessageBag(),
'total' => null,
'data' => null,
];
}
}
class EquipmentServices {
/**
* @var CategoryRepository
*/
protected $repository;
/**
* @var CategoryRepository
*/
protected $validator;
/**
* OwnersController constructor.
*
* @param CategoryRepository $repository
* @param CategoryRepository $validator
*/
public function __construct(EquipmentRepository $repository, EquipmentValidator $validator)
{
$this->repository = $repository;
$this->validator = $validator;
}
/**
* Recuperar todos os registros ativos
*
* @return array $array
*/
public function all() {
try {
$data = $this->repository->findWhere([ 'status' => 1 ])->all();
// retorna resultado da gravacao
return [
'success' => true,
'message' => null,
'total' => count($data),
'data' => $data,
];
} catch (ValidatorException $e) {
// retorna msg de erro
return [
'success' => false,
'message' => $e->getMessageBag(),
'total' => null,
'data' => null,
];
}
}
class EventServices {
/**
* @var CategoryRepository
*/
protected $repository;
/**
* @var CategoryRepository
*/
protected $validator;
/**
* OwnersController constructor.
*
* @param CategoryRepository $repository
* @param CategoryRepository $validator
*/
public function __construct(EventRepository $repository, EventValidator $validator)
{
$this->repository = $repository;
$this->validator = $validator;
}
/**
* Recuperar todos os registros ativos
*
* @return array $array
*/
public function all() {
try {
$data = $this->repository->findWhere([ 'status' => null ])->all();
// retorna resultado da gravacao
return [
'success' => true,
'message' => null,
'total' => count($data),
'data' => $data,
];
} catch (ValidatorException $e) {
// retorna msg de erro
return [
'success' => false,
'message' => $e->getMessageBag(),
'total' => null,
'data' => null,
];
}
}
Idea: Hold the inheritance of a main class with all the methods I need, however for each class, I would make an override na(s) function precisely to suit the same in the use of the class.
Is it possible to do this procedure? NOTE: The classes have different parameters in the constructor, see.
"I would do an override", and that would be different from what you’ve done now?
– Woss
Does that answer? https://answall.com/q/307092/101. And/or this? https://answall.com/q/45297/101. This should kill for good: https://answall.com/q/247363/101. This should help: https://answall.com/q/354826/101. Here is some important information: https://answall.com/q/174414/101. I gather everything I think can be duplicated.
– Maniero