|
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 |
mytto Smarty Rookie
Joined: 21 Dec 2004 Posts: 8
|
Posted: Fri Dec 15, 2006 12:41 pm Post subject: Class constants (again!) work around |
|
|
Here you'll find a work around to get access to class constants. The idea is to register a smarty modifier using ClassReflexion. Probably not the best way, nor the fastest one, but yet it works.
Code: | function smarty_modifier_const($object, $constName) {
$class = new ReflectionClass($object);
return $class->getConstant($constName);
}
|
Smarty syntax becomes :
Code: | {$object|const:"CONST_NAME"} |
_________________ Thomas Despoix
openXtrem: Solutions Open Source pour les Entrprises |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
FractalizeR Smarty Rookie
Joined: 01 Nov 2003 Posts: 16
|
Posted: Fri Dec 21, 2007 3:45 pm Post subject: |
|
|
Quote: | function smarty_modifier_const($object, $constName) {
$class = new ReflectionClass($object);
return $class->getConstant($constName);
} |
Isn't there a small mistake? ReflectionClass constructor accepts string parameter with a class name. So, your code will work only if $object->__toString returns it's class name. But in some cases __toString implementation is overriden. _________________ Things are changing,
But nothing changes,
And still there are changes |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Dec 22, 2007 12:29 am Post subject: |
|
|
You don't need to define your own modifier. The usual PHP constant() will work just as expected and as shown in ceph's follow=up posts in the thread I linked.
I also don't see the need for reflection -- if you want to query an object, consider gettings its classname and then deriving the constant name from that and pass the whole thing to constant(). In that case, a custom modifier does make sense Then again, shouldn't your code use static constant names rather than the object style you are using? |
|
Back to top |
|
FractalizeR Smarty Rookie
Joined: 01 Nov 2003 Posts: 16
|
Posted: Sat Dec 22, 2007 10:15 am Post subject: |
|
|
Yes, exactly. I just pointed out a small mistake _________________ Things are changing,
But nothing changes,
And still there are changes |
|
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
|