Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection with PHP

Dependency Injection with PHP

Bastian Hofmann

October 16, 2012
Tweet

More Decks by Bastian Hofmann

Other Decks in Programming

Transcript

  1. ?

  2. ResearchGate gives science back to the people who make it

    happen. We help researchers build reputation and accelerate scientific progress. On their terms. ‟
  3. $ wget http://getcomposer.org/composer.phar $ php composer.phar install Installing dependencies -

    Installing monolog/monolog (1.1.0) Downloading: 100% ... monolog/monolog suggests installing mlehner/ gelf-php (Allow sending log messages to a GrayLog2 server) Writing lock file Generating autoload files
  4. AOP

  5. class  Forum  {    /**      *  @FLOW3\Inject  

       *  @var  ApplicationLoggerInterface      */    protected  $applicationLogger;    /**      *  @param  Post  $post      *  @return  void      */    public  function  deletePost(Post  $post)  {        $this-­‐>applicationLogger-­‐>log('Removing  post');        $this-­‐>posts-­‐>remove($post);    } }
  6. class  Forum  {    /**      *  Delete  a

     forum  post      *      *  @param  Post  $post      *  @return  void      */    public  function  deletePost(Post  $post)  {        $this-­‐>posts-­‐>remove($post);    } }
  7. /**  *  @FLOW3\Aspect  */ class  LoggingAspect  {    /**  

       *  @FLOW3\Inject      *  @var  ApplicationLoggerInterface      */    protected  $applicationLogger;    /**      *  @FLOW3\Before("method(Forum-­‐>deletePost())")      */    public  function  logDeletePost(\TYPO3\FLOW3\AOP \JoinPointInterface  $joinPoint)  {        $post  =  $joinPoint-­‐>getMethodArgument('post');        $this-­‐>applicationLogger-­‐>log('Removing');    } }