0

I have a class that acts as a wrapper to Smarty but want to use it statically across my application.

My setup looks something like this:

class Template extends Smarty {

    public function __constructor() {

         parent::__constructor();
    }

    public function setSettings() {

         $this-> some smarty settings here
    }

    public static function loadTpl($tpl) {

         self::$tplFile = $tpl;

         // other logic

         self::setSettings(); // this won't get executed because it uses non static method calls.
    }
}

How can I get around this?

Kimi
  • 6,239
  • 5
  • 32
  • 49
Eli
  • 4,329
  • 6
  • 53
  • 78
  • 2
    A static method can only call static methods. There's no getting around this. When you get to `setSettings`, what do you expect `$this` to be? There's no associated object. – Michael Mior Jan 18 '12 at 19:01
  • 2
    Why do you want it to be called statically? Instead you could create a Smarty singleton object. – Michael Berkowski Jan 18 '12 at 19:01
  • Eli, could you please take a look: http://stackoverflow.com/questions/12436203/assigning-defaults-for-smarty-using-object-oriented-style – Ilia Ross Sep 16 '12 at 09:41

1 Answers1

0

Rather than attempt to wrap it to be called all statically, create a singleton instance and call Template::getInstance() to retrieve it rather than new Smarty():

class Template extends Smarty {
  public static $instance = NULL;

  // Private constructor can't be called
  private function __construct() {
    parent::__construct();
  }

  // Instead instantiate or return the existing instance
  public static function getInstance () {
    return self::$instance ? self::$instance : new self();
  }
}


// Instantiate as:
$smarty = Template::getInstance();
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • will that $smarty variable be accessible within other classes statically? – Eli Jan 18 '12 at 19:43
  • @s2xi Inside other classes, retrieve it as `$smarty = Template::getInstance()` and you'll have the same object, which is a static property of `Template`. So yes, it is the same static object wherever you retrieve it, but you have to call `getInstance()` rather than just use the same variable `$smarty`. – Michael Berkowski Jan 18 '12 at 19:47
  • @s2xi Wherever you call `$smarty = Template::getInstance()` you'll receive the same static instance... – Michael Berkowski Jan 18 '12 at 19:48
  • when I run this, it say that the constructor must be set to public – Eli Jan 18 '12 at 20:31
  • @s2xi What says the constructor must be public? PHP 5.1+ permits private constructors. In any case if that continues you can change to `public function __construct()` – Michael Berkowski Jan 18 '12 at 20:38
  • I changed it to public and I think I am able to create the object statically. But how would I access a static function thereafter? $smarty->getInstance()->render? – Eli Jan 18 '12 at 20:48
  • @s2xi You have an error in how you're calling a constructor. See http://www.smarty.net/forums/viewtopic.php?p=75592 – Michael Berkowski Jan 18 '12 at 20:53
  • in my class I have a custom static method that tells the smarty display function what to do – Eli Jan 19 '12 at 05:12