1

I am trying to call function but I get this error:

Fatal error: Using $this when not in object context in /home/content/34/8007634/html/independent-platform/module/Application/Module.php

Error line

            $without_login_allowed_action = $this->getWithoutLoginAllowedActionList();

Code

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;



use Zend\Authentication\AuthenticationService;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface,
Zend\ModuleManager\Feature\ConfigProviderInterface,
Zend\ModuleManager\Feature\ViewHelperProviderInterface; 


 use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

use Application\Model\Album;
use Application\Model\AlbumTable;

 use Application\Model\User;
 use Application\Model\UserTable;

 use Application\Model\Program;
 use Application\Model\ProgramTable;
 use Zend\Db\Adapter\Adapter;

 use Zend\Session\SessionManager;
 use Zend\Session\Container;

 class Module implements AutoloaderProviderInterface,     ConfigProviderInterface, ViewHelperProviderInterface
 {






 //...handle the exception... maybe log it and redirect to another page,
 //or send an email that an exception occurred...

 public function handleError(MvcEvent $e)
 {
    //get the exception
    $exception = $e->getParam('exception');

    $response = $e->getResponse();
    $response->setStatusCode(404);
    $response->sendHeaders();
 }

public function onBootstrap(MvcEvent $e)
{
    ini_set('memory_limit', '-1');

    $app = $e->getApplication();


        $without_login_allowed_action = $this->getWithoutLoginAllowedActionList();
        // Route is whitelisted





    $this->bootstrapSession($e);

}


public function getWithoutLoginAllowedActionList(){
    $without_login_allowed_action = array(
                    'index',
                    'login',
                    'register',
                    'program',
                    'thanks',
                    'logout',
                    'forgotpass',
                    'resetpassword',
                    'checkMonthlySubscription',
                    'consultantform',
                    'checkForTestingStarted',
                    'emailcron',
                    'flushemailqueue',
                    'rtuactivity',
                    'blogslider',
                    'contact',
                    'latestnews',
                    'privacypolicy',
                    'news',
                    'generatemathcaptcha',
                    'getprofileimagewithoutlogin',
                    'getprogramimagebyprofilewithoutlogin',
                    'wlcinquiry',
                    'normalchaineduserregister',            
    );
    return $without_login_allowed_action;
}
  } 
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • 2
    http://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context – Durim Jusaj Aug 26 '15 at 14:52
  • The answer is in the error. You're using `$this` outside a class definition. – Jonnix Aug 26 '15 at 14:55
  • Hey, thanks for the reply. but I didn't defined function outside the class. can you pls check above code.. – heryali kulkarni Aug 26 '15 at 15:04
  • Are your sure the line causing the error isn't wrapped inside an anonymous function? Looking at the code you've posted you've clearly edited it a lot to remove unnecessary/irelevant lines, and in doing so it's unclear the context surrounding that call. Perhaps you're attaching a listener and your erroneous method call is inside the anonymous function containing your listener logic? The indentation tells me something like that is going on here, which is why people are having a hard time finding a solution. Add the lines of code surrounding before and after the problem call, it might help. – Crisp Aug 27 '15 at 19:00

1 Answers1

0

In this context it means, the method onBootstrap is called statically, i.e. as Module::onBootstrap($e)

Either make sure that this does not happen, or change the methods accordingly (they don't operate on the instance anyway):

  1. Declare them as static:

    public static function onBootstrap(MvcEvent $e)
    
    public function getWithoutLoginAllowedActionList()
    
  2. Change the method call within the class to use self instead of $this:

        $without_login_allowed_action = self::getWithoutLoginAllowedActionList();
    
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • hi thanks. I have changed my code as per your comment but now gives me below error..............................Fatal error: Uncaught exception 'Zend\Stdlib\Exception\InvalidCallbackException' with message 'Invalid callback provided; not callable' – heryali kulkarni Aug 26 '15 at 15:12
  • So the method is called elsewhere non-statically. I don't know the ZF2 ModuleManager well enough to tell you, where and why – Fabian Schmengler Aug 26 '15 at 15:21