oop - How to make PHP libraries loosely coupled? -


i have libraries in 'framework' routing, config, logger,... want them independent of each other, of known php frameworks make them.

i understand principles of loose coupling, have no clue how follow both loose coupling , dry principles. if make routing library config , logger, don't repeat myself, if want use router on own won't work. if write logging , config code routing library, repeat myself.

loose coupling means components not expect concrete instance 1 instance has compatible interface.

each collaborator can replaced different 1 of same type. code not dependent on concrete implementation of 1 of longer.

so:

  • do not use:

    • global (static) functions

      foo:bar(); 
    • class based programming (passing 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 mocks

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

see well:


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -