How to manage complex class dependencies in PHP?

30
March 16, 2019, at 05:20 AM

I'm writing a PHP script that generates SVG plans for all the components that make up a pinball machine, to send to a laser cutter. There are many dozens of components. Each one is defined in a separate class file, something like this:

class CabinetFrontComponent extends Component {
    public $width;
    public $height;
    function __construct() 
    {
      /* 
       * Geometry of each component is calculated in its constructor. For 
       * the sake of simplifying this example, I'll just set these two
       * properties to a numeric value.
       */
      $this->width = 150;
      $this->height = 150; 
    }
    function draw()
    {
      // Draws the component and writes an SVG file to disk    
    }
  }

The components are all loaded and drawn like this:

foreach (glob(__DIR__ . "/components/*.php") as $filename) {
  require_once($filename);
}
foreach( get_declared_classes() as $class ){
  if( is_subclass_of( $class, 'Component' ) ) {
    $obj = new $class;
    $obj->draw();
  }
}

The problem is that some components' geometry can't be calculated until another component has first been calculated. For example, the dimensions of the CabinetSidesComponent can't be calculated until we know the PlayfieldComponent dimensions, and that component can't be calculated until we know the BumperComponent dimensions.

Is there some way I can tell PHP to instantiate these in the right order, so that all the necessary dependencies are met? I mean, short of manually specifying the order to load them?

Also, once the components have been instantiated, what is the best way to make their properties accessible from other objects?

READ ALSO
How can I insert a new column in my table using php and mysql?

How can I insert a new column in my table using php and mysql?

I'm trying to add the column "test" in my table "reservation" but it doesn't work

20
Getting error when trying to edit subpage in Grav

Getting error when trying to edit subpage in Grav

Whenever I try to add or edit a subpage (ie

45
Zend Expressive API does not return contents of objects

Zend Expressive API does not return contents of objects

I'm creating a small API, mostly for learning purposes, but, I might implement it into a project I'm working onSo far, I have installed the zend expressive skeleton application and set up my models and entities

11
What change Language codeigniter

What change Language codeigniter

I want to rend my codeigniter multilanguage and I would create a button that it can change in config/configphp $config[language]== "? "

22