# Reuqest object null when passed

**URL:** <https://discourse.slimframework.com/t/reuqest-object-null-when-passed/1290>\
**Category:** Questions\
**Created:** [March 18, 2017, 3:02pm UTC](https://discourse.slimframework.com/t/reuqest-object-null-when-passed/1290 "2017-03-18T15:02:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coredump](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@coredump](https://discourse.slimframework.com/u/coredump)\
**Post date:** [March 18, 2017, 3:02pm UTC](https://discourse.slimframework.com/t/reuqest-object-null-when-passed/1290/1 "2017-03-18T15:02:45Z")

</div>

This question may need to go to a different forum but I’ll ask here just in case. I am trying to pass the Request object to the constructor of a base class. In the main index file it shows the variable as valid but when I pass it to the base class constructor it says that the object is NULL. So the obvious question is what am I doing wrong? but also should I be using something in the framework to accomplish this same behavior (routing ?)

Here is the abbreviated code

(Index.php)

$app-\>post(’/Service’, function (Request $request, Response $response) {

```
$baseObj = new BaseEntity($request, $response);

return $response;

```

});

(BaseEntity.php)

class BaseEntity {  
public function \_\_construct(Request $req, Response $resp) {  
$this-\>request = $req;  
$this-\>response = $resp;  
$this-\>rawjson = $req-\>getParsedBody();  
}

```
            private $request;
            private $response;
            private $rawjson;

```

}

and this is the error I receive

Type: TypeError  
Message: Argument 1 passed to BaseEntity::\_\_construct() must be an instance of Request, instance of Slim\Http\Request given,

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [March 18, 2017, 4:51pm UTC](https://discourse.slimframework.com/t/reuqest-object-null-when-passed/1290/2 "2017-03-18T16:51:27Z")

</div>

Since you are type hinting `Request` you will need to say where to find it in `BaseEntity.php`. (And then it won’t know what `Response` is after that is fixed, so you will likely want to add that too.)

```php
<?php

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class BaseEntity
{
    // ...
}

```

---

<div class="post-metadata">

**Author:** ![coredump](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@coredump](https://discourse.slimframework.com/u/coredump)\
**Post date:** [March 18, 2017, 6:19pm UTC](https://discourse.slimframework.com/t/reuqest-object-null-when-passed/1290/3 "2017-03-18T18:19:12Z")

</div>

> [@tflight](#):
>
> use Psr\Http\Message\ServerRequestInterface as Request;  
> use Psr\Http\Message\ResponseInterface as Response;

Now I get this error:

Type: TypeError  
Message: Argument 1 passed to BaseEntity::\_\_construct() must implement interface Psr\Http\Message\ServerRequestInterface, none given,

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [March 18, 2017, 6:39pm UTC](https://discourse.slimframework.com/t/reuqest-object-null-when-passed/1290/4 "2017-03-18T18:39:11Z")

</div>

Someone else might be able to infer what is going on, but I’d need to see a bit more of your code and/or your setup as `Slim\Http\Request` does implement `ServerRequestInterface`, which from the previous error does seem to be the object passed.

---

<div class="post-metadata">

**Author:** ![coredump](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@coredump](https://discourse.slimframework.com/u/coredump)\
**Post date:** [March 18, 2017, 6:53pm UTC](https://discourse.slimframework.com/t/reuqest-object-null-when-passed/1290/5 "2017-03-18T18:53:28Z")

</div>

////////////////////////////////////////////////////////////  
index.php  
///////////////////////////////////////////////////////////

\<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; require '../vendor/autoload.php'; require\_once 'utility\_functions.php'; require\_once 'Endpoint/BaseEntity.php' $app = new \Slim\App; $app-\>post('/Service', function (Request $request, Response $response) { $baseObj = new BaseEntity($request, $response); return $response; }); $app-\>post('/Service2', function (Request $request, Response $response) { }); $app-\>post('/Service3', function (Request $request, Response $response) { }); $app-\>run(); ?\>

/////////////////////////////////////////////////////////////////////////////  
BaseEntity.php  
/////////////////////////////////////////////////////////////////////////////

\<?php use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; class BaseEntity { public function \_\_construct(Request $req, Response $resp) { $this-\>request = $req; $this-\>response = $resp; $this-\>rawjson = $req-\>getParsedBody(); } private $request; private $response; private $rawjson; } ?\>

////////////////////////////////////////////////////////  
File Structure  
//////////////////////////////////////////////////////

~/project/src  
composer.json  
composer.lock  
vendor

~/project/src/public/  
index.php  
utility\_functions.php

~/project/src/public/Endpoint/  
BaseEntity.php
