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

Dependency Injection with PHP

Dependency Injection with PHP

Bastian Hofmann

July 18, 2012
Tweet

More Decks by Bastian Hofmann

Other Decks in Programming

Transcript

  1. ?

  2. $ 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
  3. AOP

  4. 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);    } }
  5. class  Forum  {    /**      *  Delete  a

     forum  post      *      *  @param  Post  $post      *  @return  void      */    public  function  deletePost(Post  $post)  {        $this-­‐>posts-­‐>remove($post);    } }
  6. /**  *  @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  post);    } }