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

PHP5's MyObject::CONST syntax?

 
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
Ceph
Smarty Rookie


Joined: 25 Nov 2003
Posts: 24

PostPosted: Tue Aug 09, 2005 4:37 am    Post subject: PHP5's MyObject::CONST syntax? Reply with quote

Code:

{if $page->GetClient()->status_type == Client::STATUS_DEMO}


syntax error: unidentified token '::STATUS_DEMO' (Smarty_Compiler.class.php, line 1384)

Also, $page->GetClient()->status_type compiles to:

elseif ($this->_tpl_vars['page']->GetClientWebsite() - > status_type

I know Smarty doesn't officially support PHP5, but any idea how I could fix either of those?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue Aug 09, 2005 4:46 am    Post subject: Reply with quote

Perhaps use a config file. Since config files support custom resources you might want to write one that introspects your class instead of duplicating the constant definitions in both your class and a real config file. That way you should be able to do something like:

{config_load file="ClassConstants:Client"}
{if $foo == #STATUS_DEMO#}

I haven't actually tried (or needed) such a thing but it should work well enough.

Perhaps a little bit simpler, you can extend your custom Smarty class and add a method that introspects a given class and assigns it to a standard array that you can define:

$smarty->assignClassConstants('Client');
---
{if $foo == $const.Client.STATUS_DEMO}

or something along those lines.

As for page->GetClient()->status_type, please search the forum for 'derefrencing'.
Back to top
View user's profile Send private message
Ceph
Smarty Rookie


Joined: 25 Nov 2003
Posts: 24

PostPosted: Tue Aug 09, 2005 4:59 am    Post subject: Reply with quote

The constants issue isn't a biggie, as you said. I did write a simple converter method in my derived Smarty object to convert all class constants to real constants.

However, Smarty breaking the object code isn't very nice. Smile

I can only resort to:

{assign var="client" value=$page->GetClient()}
{if $client->status_type ...

which works for now I guess. Will a "5" version be released?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue Aug 09, 2005 5:49 am    Post subject: Reply with quote

Well... you may think of it of breaking code or you can think of it as supporting PHP4 style objects which is what Smarty 2.x is designed to do. My guess is that Smarty 3.x will address the issue of PHP5 style objects.
Back to top
View user's profile Send private message
Ceph
Smarty Rookie


Joined: 25 Nov 2003
Posts: 24

PostPosted: Tue Aug 09, 2005 12:46 pm    Post subject: Reply with quote

In case anyone else needs to use class constants in PHP5/Smarty 2 series:

Code:

{if $client->GetStatusCode() == constant("Client::STATUS_SUSPENDED")}


works fine, as does:

Code:

{"Client::STATUS_SUSPENDED"|constant}
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue Aug 09, 2005 3:36 pm    Post subject: Reply with quote

nice one, ceph!
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 -> 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