Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1070045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:30:24+00:00 2026-05-16T20:30:24+00:00

Here is my class that gets called on each page: class ActionHandler { var

  • 0

Here is my class that gets called on each page:

class ActionHandler {

    var $smarty = NULL;

    public function __construct() {

        if($this->smarty == NULL){

            $this->smarty = new Smarty();

            $this->smarty->template_dir = TEMPLATE_DIR;

            $this->smarty->compile_dir = COMPILE_DIR;

        }

    }

    public function do_something($page_id) {
        return $page_id + 1;
    }
}   

Now I have a custom plugin for smarty that I want to use in my template:

function smarty_function_something($params, &$smarty) {
    return ActionHandler::do_something($params['page_id']);
}

However I get Fatal error: Using $this when not in object context.

I see why but don’t know how to get around this. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:30:25+00:00Added an answer on May 16, 2026 at 8:30 pm

    Try making the do_something a static member of ActionHandler

    class ActionHandler {
    
        public static $smarty = NULL;
        public function __construct()
        {
            if($this->smarty == NULL)
           {
                $this->smarty = new Smarty();
                $this->smarty->template_dir = TEMPLATE_DIR;
                $this->smarty->compile_dir = COMPILE_DIR;
    
           }
       }
    
        public static function do_something($page_id)
        {
            return $page_id + 1;
        }
    }
    

    As your trying to access a non static method i *think that the __construct gets executed before the method is available, but as you have not created an instance of the object, the keyword $this does not exists.

    you have to create specific static methods. if your going MyObject::SomeMethod($param)

    you should also take a look at Object Auto Loading and Auto Initializing objects via static methods.

    also you don’t need to specifically define the value to public static $smarty = NULL; as Null is a default value of any new variable, just do

    public static $smarty;
    

    going a little more indepth with your problem you should add a singleton method like so..

    class ActionHandler
    {
        public static $smarty;
        public static $singleton;
        public function __construct()
        {
            if($this->smarty == NULL)
           {
                $this->smarty = new Smarty();
                $this->smarty->template_dir = TEMPLATE_DIR;
                $this->smarty->compile_dir = COMPILE_DIR;
    
           }
       }
        public static GetSingleton()
        {
            if(self::$singleton == null)
            {
                 self::$singleton = new ActionHandler();
            }
            return self::$singleton;
        }
    
        public static function do_something($page_id)
        {
            $_this = self::GetSingleton();
    
    
            return $page_id + 1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this class public static class MyClass { static MyClass() { ...
I'm trying to create a custom view GhostSurfaceCameraView that extends SurfaceView . Here's my
I am writing a numerical program that should be somewhat fast, and is also
I have a layer-hosting view set up like this in a custom NSView subclass:
I have a java program that I am trying to generate 3 outputs for,
I have a base class and a class inheriting base. The base class has
I have to execute some code in the context of the main thread. I
I am translating a compact framework 2.0 windows form in Visual Studio 2005. To
I'm working on a java project with my team at work. To summarize, we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.