Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pradeep-rajapaksha's avatar

Calling repository methods each other

I was trying to call a function/method in a repository class (via services class), got the php memory limit error, and could not resolve the issue. However, I have been able to find out that the repository class I was trying to access has been injected my current repository class already. My question is, can't we call methods across the classes?

This is what I'm trying to do simply.

class FristRepository implements FristRepositoryInterface
{
    public function __construct( 
        SecondRepositoryServiceInterface $secondRepositoryService)
    {
        $this->secondRepositoryService = $secondRepositoryService;
    }

    public function myFirstFunction () {
        $this->secondRepositoryService->myThirdFunction();
    }

    public function mySecondFunction () {
        // 
    }
}

class SecondRepository implements SecondRepositoryInterface
{
    public function __construct( 
        FristRepositoryServiceInterface $firstRepositoryService)
    {
        $this->firstRepositoryService = $firstRepositoryService;
    }

    public function myThirdFunction () {
        // 
    }

    public function myForthFunction () {
        $this->firstRepositoryService->mySecondFunction();
    }
}
0 likes
4 replies
Snapey's avatar

Sorry but I look at your code and .... what the hell?

Seems like you are getting yourself in circular reference hell. First repository needs an instance of second repository needs an instance of first repository needs an instance of second repository etc etc. Who needs code that badly structured?

2 likes
martinbean's avatar

My question is, can't we call methods across the classes?

@pradeep-rajapaksha Nope. Repositories should not know about each other or be calling each other. A repository should only be concerned about one particular class of entity. An ArticleRepository should only fetch articles; a CategoryRepository should only fetch categories, and so on.

Fetching data via multiple repositories is something a service class should orchestrate.

2 likes
pradeep-rajapaksha's avatar

@martinbean Thank you for the feedback. Yes, I know we cannot call repositories directly, so I was injecting repository service interfaces. However, It couldn't call from the services level too. Had to do that from the controller level. I guess that's the correct way to do that.

martinbean's avatar

I know we cannot call repositories directly, so I was injecting repository service interfaces.

@pradeep-rajapaksha That’s still trying to use a repository within a repository, which you shouldn’t be doing. And in your example, you were making “first repository” require “second repository” and “second repository” require “first repository”, creating a cyclic dependency.

So again, you should be using service classes to orchestrate fetching data from multiple repositories:

class ArticleService
{
    public function __construct(
        protected ArticleRepository $articles,
        protected CategoryRepository $categories,
    ) {}

    public function getArticleWithCategoryById(string $id): Article
    {
        $article = $this->articles->findById($id);
        $category = $this->categories->findById($article->category_id);

        $article->setCategory($category);

        return $article;
    }
}

Please or to participate in this conversation.