Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Leonid Mamchenkov:
Dependency resolution with graphs in PHP
Nov 22, 2016 @ 16:52:23

Leonid Mamchenkov has a post to his site showing how he solved an interesting problem in one of his recent projects: determining the order to use items based on their dependencies.

One of the projects I am working on at work presented an interesting problem. I had a list of items with dependencies on one another and I needed to figure out the order in which to use those items, based on their dependencies.

He gives the example of database tables where it would be required to export the tables so that the relations between them are maintained when imported back in. He gives some example data, a basic nested PHP array, and defines the relationships between them (just strings in this case). While he did solve the problem, he wasn't happy with the solution. Instead he went looking for other options and found graph theory to be a good match. He briefly cover what the theory involves and links to an example that basically does what he needs...but is written in Python. He finishes off the post sharing his refactoring of this logic into PHP including a recursive "dependency resolver" and the output showing the correct ordering for loading objects based on their dependencies.

tagged: resolve dependency graph theory example tutorial load order

Link: http://mamchenkov.net/wordpress/2016/11/22/dependency-resolution-with-graphs-in-php/


Trending Topics: