Call to a member function write() on null

getting this error
Notice: Undefined variable: response in F:\xampp\xxampp\htdocs\RESTAPI\test\middleware.php on line 12
Fatal error: Call to a member function write() on null in F:\xampp\xxampp\htdocs\RESTAPI\test\middleware.php on line 12

//this middleware class

<?php namespace middleware; /** * middleware */ class guestMiddleware { public function __invoke($Request, $Response, $next) { $response->write("less"); $response = $next($request, $response); $response->write("less"); return $response; } } ?>

//route code

$app->group(’/’,function() use($app){

$app->get('getmail/{email}',function ($request, $response, $args){
//$obj = new ps;
$args['email'];
	$status = $response->getStatusCode();
  return $response;

});
})->add(new middleware\guestMiddleware());

$app->run();

PHP variables are case sensitive. $Request is different than $request. Try this.

public function __invoke(Request $request, Response $response, $next)

Or if you don’t wish to type hint:

public function __invoke($request, $response, $next)

To add a bit on what @tflight said, note that while variables are case sensitive, functions and classes (not objects when used as variables, careful) are case insensitive.

Constants are case sensitive by default but take a look at this :

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

It’s from the PHP manual. You may define constants as case insensitive, if you’d like.

This is just a heads up. I’ve had quite a bit of headache from peculiarity in the language. The problem you’ve had here is a consequence of this language specific.

1 Like