|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
SMURF Smarty Rookie
Joined: 18 Jan 2005 Posts: 5
|
Posted: Tue Jan 18, 2005 10:45 pm Post subject: Frage zu Klassenaufruf innerhalb einer smarty function |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Jan 19, 2005 9:30 am Post subject: |
|
|
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 ) in dem Fall mal nach "singleton design pattern php". |
|
Back to top |
|
SMURF Smarty Rookie
Joined: 18 Jan 2005 Posts: 5
|
Posted: Wed Jan 19, 2005 10:54 am Post subject: |
|
|
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 |
|
Back to top |
|
|
|
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
|