Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have circular dependencies in Composer?

I'm writing a package A that is required by some other package B which I'm not publishing for now. At some point A will be changed to use itself B. Chances are they should be both in the same package then, but I'd prefer keeping the two things separate, just for cleanness' sake, and, more importantly, because B is only a dev dependency to A.

package A requires-dev B
package B requires A

I'm curious if this is possible. I'm also curious if it's the same for:

package A requires B
package B requires A

...and...

package A requires B
package B requires C
package C requires A

... or more complex cases. What problems will I encounter?

Thank you.

like image 733
Wes Avatar asked Oct 22 '25 02:10

Wes


1 Answers

A wider, not php-specific answer here: circular dependencies are never a good idea.

You see, you "separate" things into different packages/modules/you-name-it in order to give them a useful structure. To create a "model" that helps you dealing with the complexity of your code.

In other words: you want to define an architecture. And circular dependencies are most often seen as "bad smell" in designs.

Thus you shouldn't ask "would it work?", but "is there a better way to handle this?"

like image 107
GhostCat Avatar answered Oct 24 '25 16:10

GhostCat