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

Class constants (again!) work around

 
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 -> General
View previous topic :: View next topic  
Author Message
mytto
Smarty Rookie


Joined: 21 Dec 2004
Posts: 8

PostPosted: Fri Dec 15, 2006 12:41 pm    Post subject: Class constants (again!) work around Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Dec 18, 2006 6:31 pm    Post subject: Reply with quote

see also: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5923
Back to top
View user's profile Send private message
FractalizeR
Smarty Rookie


Joined: 01 Nov 2003
Posts: 16

PostPosted: Fri Dec 21, 2007 3:45 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Dec 22, 2007 12:29 am    Post subject: Reply with quote

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 Smile Then again, shouldn't your code use static constant names rather than the object style you are using?
Back to top
View user's profile Send private message
FractalizeR
Smarty Rookie


Joined: 01 Nov 2003
Posts: 16

PostPosted: Sat Dec 22, 2007 10:15 am    Post subject: Reply with quote

Yes, exactly. I just pointed out a small mistake Wink
_________________
Things are changing,
But nothing changes,
And still there are changes
Back to top
View user's profile Send private message Send e-mail
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 -> General 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