title: Call a function on each http request
body: <p>In my Apigility project I have different Rest resources, all of them extends my class ResourseAbstract and in there I extend the AbstractResourceListener as Apigility needs. </p>
  
  <p>So for example my resource User: </p>
  
  <pre><code>&lt;?php
  namespace Marketplace\V1\Rest\User;
  
  use ZF\ApiProblem\ApiProblem;
  use Marketplace\V1\Abstracts\ResourceAbstract;
  
  class UserResource extends ResourceAbstract
  {
      public function fetch($id)
      {
          $result = $this-&gt;getUserCollection()-&gt;findOne(['id'=&gt;$id]);
          return $result;
      }
  }
  </code></pre>
  
  <p>And ResourceAbstract:</p>
  
  <pre><code>&lt;?php
  
  namespace Marketplace\V1\Abstracts;
  
  use ZF\Rest\AbstractResourceListener;
  use Zend\ServiceManager\ServiceLocatorAwareInterface;
  use Zend\ServiceManager\ServiceLocatorInterface;
  use ZF\ApiProblem\ApiProblem;
  
  class ResourceAbstract extends AbstractResourceListener implements ServiceLocatorAwareInterface {
  
  }
  </code></pre>
  
  <p>Now, I need to run a function each time an http request is made, if I query /user in my browser the UserResource class will get instantiated and so the ResourceAbstract, my "solution" to get something to run on each call was to use a constructor inside ResourceAbstract, and this "works":</p>
  
  <pre><code>function __construct() {
      $appKey = isset(getallheaders()['X-App-Key']) ? getallheaders()['X-App-Key'] : null;
      $token = isset(getallheaders()['X-Auth-Token']) ? getallheaders()['X-Auth-Token'] : null;
      //some code
      return new ApiProblem(400, 'The request you made was malformed');
  }
  </code></pre>
  
  <p>The thing is I need to return an ApiProblem in some cases (bad headers on the http request), but as you know constructor function does not return parameters. Another solution will be to thrown an exception but in Apigility you are supposed to ise ApiProblem when there is an api problem. Is the constructor approach correct? How will you solve this? </p>