POO in PHP classes ( reuse methods and attributes from other inherited classes)

Asked

Viewed 100 times

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?

  • 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.

1 answer

0


One way would be to implement the method in a generic way trait:

trait FindAll
{
    public function findAllWhere($where)
    {
        try {
            $data = $this->repository->findWhere($where)->all();

            return [
                'success'   => true,
                'message'   => null,
                'total'     => count($data),
                'data'      => $data,
            ];

        } catch (ValidatorException $e) {
            return [
                'success'   => false,
                'message'   => $e->getMessageBag(),
                'total'     => null,
                'data'      => null,
            ];
        }
    }
}

And in their classes use the trait and define the methods all specializing in the method findAllWhere:

class CategoryServices {

    use FindAll;    

    // ...

    public function all() {
        return $this->findAllWhere(['status' => 1]);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.