Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Frage zu Klassenaufruf innerhalb einer smarty function

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
SMURF
Smarty Rookie


Joined: 18 Jan 2005
Posts: 5

PostPosted: Tue Jan 18, 2005 10:45 pm    Post subject: Frage zu Klassenaufruf innerhalb einer smarty function Reply with quote

hi,

ich möchte gerne eine Klasse innerhalb einer smarty function aufrufen. z.b.

$class = new Class;

wird jetzt im smarty template eine function aufgerufen, die auf die gleiche Klasse zugreift soll diese nicht nochmals instanziiert werden. Fällt jemandem eine geschickte Lösung ein?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jan 19, 2005 9:30 am    Post subject: Reply with quote

Also wenn deine Class schon ab und zu mehrfach verwendet werden soll, nur aus deiner Smarty Function nicht, dann kannst du eine statische Variable verwenden. siehe http://php.net/manual/en/language.variables.scope.php#static

[php:1:cd14c7e570]
static $class;
if (!isset($class)) $class = new Class;
[/php:1:cd14c7e570]

Wenn deine Class allerdings wirklich immer nur einmalig instanziert werden darf/soll, weil alles andere keinen Sinn macht, dann solltest du Class als Singleton implementieren. Google (oder yahooe Smile ) in dem Fall mal nach "singleton design pattern php".
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SMURF
Smarty Rookie


Joined: 18 Jan 2005
Posts: 5

PostPosted: Wed Jan 19, 2005 10:54 am    Post subject: Reply with quote

hi,

danke für den Hinweis. Habs jetzt so gelöst:

zunächst Grundfunktionalität von Smarty erweitert.

Code:

class Template extends Smarty
{
   function Template()
   {
   $this->Smarty();
   }

   function singleton($className)
   {
      switch($className)
      {
         case "Category":   
         {
            if(!isset($this->category))
            {
            require_once(dirname(__FILE__)."/Category.class.php");
            $this->category = new Category;
            }
         return $this->category;
         break;
         }
         default:   
         {
         return false;
         }
      }
   }
}


Anschließend in smarty plugin function eingebaut:

Code:

function smarty_function_categoryGetTree($params, &$smarty)
{
$category = &$smarty->singleton("Category");

$categories = $category->categoryGetTree($_GET["id"]);

$smarty->assign("category",&$categories);
}


Weiß zwar nicht, ob das dem Designmuster entspricht aber es funktioniert Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP