Link to home
Start Free TrialLog in
Avatar of Dima Iva
Dima Iva

asked on

How to fix "Using $this when not in object context" in php error log?

I have an OJS journal running on Apache/2.4.35 (Win32) OpenSSL/1.1.0i PHP/7.2.11.

In order to fix a "non-static method PKPApplication::getRequest()" error:

PHP Deprecated:  Non-static method PKPApplication::getRequest() should not be called statically in C:\xampp\htdocs\journal\plugins\generic\customHeader\CustomHeaderPlugin.inc.php on line 128

Open in new window


I ended up adding the word "statitc" in two files:

on line 237 in C:\xampp\htdocs\journal\lib\pkp\classes\core\PKPApplication.inc.php

233:    /**
234:     * Get the request implementation singleton
235:     * @return Request
236:     */
237:    public static function getRequest() {
238:       $request =& Registry::get('request', true, null); // Ref req'd
239: 
240:       if (is_null($request)) {
241:          import('classes.core.Request');
242: 
243:          // Implicitly set request by ref in the registry
244:          $request = new Request();
245:       }
246: 
247:       return $request;
248: 

Open in new window



and on line 143 in C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php

138:    /**
139:     * Return PKP request object
140:     *
141:     * @return PKPRequest
142:     */
143:    public static function getRequest() {
144:       return $this->_request;
145:    }

Open in new window


I now have the following error in php_error_log:

Slim Application Error:
Type: Error
Message: Using $this when not in object context
File: C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php
Line: 144

Open in new window


The whole error is here:
Slim Application Error:
Type: Error
Message: Using $this when not in object context
File: C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php
Line: 144
Trace: #0 C:\xampp\htdocs\journal\lib\pkp\classes\security\authorization\internal\ApiAuthorizationMiddleware.inc.php(39): APIHandler::getRequest()
#1 C:\xampp\htdocs\journal\lib\pkp\classes\security\authorization\internal\ApiAuthorizationMiddleware.inc.php(72): ApiAuthorizationMiddleware->_authorize(Object(Slim\Http\Request))
#2 [internal function]: ApiAuthorizationMiddleware->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Slim\App))
#3 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(ApiAuthorizationMiddleware), Array)
#4 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Slim\App))
#5 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Slim\App))
#6 C:\xampp\htdocs\journal\lib\pkp\classes\security\authorization\internal\ApiCsrfMiddleware.inc.php(46): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#7 [internal function]: ApiCsrfMiddleware->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#8 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(ApiCsrfMiddleware), Array)
#9 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#10 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#11 C:\xampp\htdocs\journal\lib\pkp\classes\security\authorization\internal\ApiTokenDecodingMiddleware.inc.php(82): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#12 [internal function]: ApiTokenDecodingMiddleware->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#13 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(ApiTokenDecodingMiddleware), Array)
#14 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#15 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#16 C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php(68): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#17 [internal function]: APIHandler->{closure}(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#18 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)
#19 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#20 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#21 C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php(127): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#22 [internal function]: APIHandler->{closure}(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#23 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)
#24 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#25 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#26 C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php(132): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#27 [internal function]: APIHandler->{closure}(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#28 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)
#29 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#30 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#31 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#32 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(APIResponse))
#33 C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php(124): Slim\App->process(Object(Slim\Http\Request), Object(APIResponse))
#34 [internal function]: APIHandler->{closure}(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#35 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)
#36 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#37 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#38 C:\xampp\htdocs\journal\lib\pkp\classes\handler\APIHandler.inc.php(132): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#39 [internal function]: APIHandler->{closure}(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#40 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)
#41 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#42 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(APIResponse), Object(Closure))
#43 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(APIResponse))
#44 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(APIResponse))
#45 C:\xampp\htdocs\journal\lib\pkp\lib\vendor\slim\slim\Slim\App.php(297): Slim\App->process(Object(Slim\Http\Request), Object(APIResponse))
#46 C:\xampp\htdocs\journal\lib\pkp\classes\core\APIRouter.inc.php(115): Slim\App->run()
#47 C:\xampp\htdocs\journal\lib\pkp\classes\core\Dispatcher.inc.php(143): APIRouter->route(Object(Request))
#48 C:\xampp\htdocs\journal\lib\pkp\classes\core\PKPApplication.inc.php(281): Dispatcher->dispatch(Object(Request))
#49 C:\xampp\htdocs\journal\index.php(68): PKPApplication->execute()
#50 {main}
View in rendered output by enabling the "displayErrorDetails" setting.

Open in new window


I understand that I probably needed to add "static" higher up in the APIHandler.inc.php file, but I am not sure where.  And maybe I don't need "static" in the PKPApplication.inc.php?  I am attaching the file referred in the error.  Could someone point me in the right direction?

APIHandler.inc.php

Many thanks in advance!
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi,

makeing getRequest() static is definitely wrong - any method declared as static is accessible without the creation of an object. Static functions are associated with the class, not an instance of the class. They are permitted to access only static methods and static variables ... this means, there's simply no $this which can be used in a method which is declared as static.

IMO the real problem is how you use this function/class ... from the first error shown above it seems you directly call PKPApplication::getRequest(), instead you'll have to either create (or use an already existing) instance of the type PKPApplication, and call getRequest() against it.

I think the places where this fails are the lines like this (lines 112, 128 and 146):
$request = Application::getRequest();

Open in new window

I'm not sure (since I don't know the classes), but I think the correct way to use getRequest() is shown some lines below (line 134):
$this->_request = Application::get()->getRequest();

Open in new window


So, with a bit luck it should work when you replace all calls like Application::getRequest() with Application::get()->getRequest() and remove the static from the declaration of getRequest().

Hope this helps,

ZOPPO
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial