"how to make php libraries loosely coupled? [closed]" Code Answer

5

loose coupling normally means that your components do not expect a concrete instance but just one instance that has a compatible interface.

each collaborator can be replaced then with a different one of the same type. the code is not dependent on a concrete implementation of one of those any longer.

so:

  • do not use:

    • global (static) functions

      foo:bar();
      
    • class based programming (passing a classname around)

      stream_wrapper_register("var", "variablestream");
      
    • global constants

      if ( !defined('abspath') )   
          define('abspath', dirname(__file__) . '/');
      
  • but:

    • use objects

      $foo->bar();
      
    • program against interfaces

      public function __construct(loggerinterface $logger) {
      
    • unit-test with mocks

      $logger = $this->getmock('loggerinterface', array('log'));
      

see as well:

  • don't be stupid: grasp solid! (dec 2011; by nikic)
By Rohit_D on March 1 2022

Answers related to “how to make php libraries loosely coupled? [closed]”

Only authorized users can answer the Search term. Please sign in first, or register a free account.