simpli Posted June 15, 2009 Share Posted June 15, 2009 Hi, I want to declare an array constant to use as JSON header. I do the following but I get an error. class Model_DbTable_Tree extends Zend_Db_Table_Abstract { protected $_name = 'tree'; protected $_primary = 'node_id'; const $_JSON_HEADER = array("label" => "node_name", "identifier" => "node_id"); but I get an error message: Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /Library/WebServer/Documents/budgetobjects/application/models/DbTable/Tree.php on line 8 I'm using Zend 1.8. Can anyone tell me how to declare that constant properly? Thanks, JR Quote Link to comment https://forums.phpfreaks.com/topic/162223-solved-declaring-array-constant/ Share on other sites More sharing options...
MadTechie Posted June 15, 2009 Share Posted June 15, 2009 Constants differ from normal variables in that you don't use the $ symbol to declare or use them. Quote Link to comment https://forums.phpfreaks.com/topic/162223-solved-declaring-array-constant/#findComment-856145 Share on other sites More sharing options...
simpli Posted June 15, 2009 Author Share Posted June 15, 2009 Hi, thank you for the precision. It seems there's no way to declare a const array: I get the error message: Arrays are not allowed in class constants in Is there a workaround to that? Thanks, JR Quote Link to comment https://forums.phpfreaks.com/topic/162223-solved-declaring-array-constant/#findComment-856164 Share on other sites More sharing options...
MadTechie Posted June 15, 2009 Share Posted June 15, 2009 you could define and initialize an static array variable: ie class Model_DbTable_Tree extends Zend_Db_Table_Abstract { protected $_name = 'tree'; protected $_primary = 'node_id'; public static $_JSON_HEADER = array("label" => "node_name", "identifier" => "node_id"); } Quote Link to comment https://forums.phpfreaks.com/topic/162223-solved-declaring-array-constant/#findComment-856176 Share on other sites More sharing options...
themarty Posted June 30, 2009 Share Posted June 30, 2009 This class comes very close to how you would expect constant arrays to work if they would have been implemented in PHP: http://devshed.excudo.net/scripts/php/source/constant+arrays There's also an example page showing a short demonstration Quote Link to comment https://forums.phpfreaks.com/topic/162223-solved-declaring-array-constant/#findComment-866263 Share on other sites More sharing options...
Mark Baker Posted June 30, 2009 Share Posted June 30, 2009 define the constant as a serialized array, then unserialize when you use it: define('MyConst','a:3:{i:0;s:16:"This is an array";i:1;s:20:"but it is serialized";i:2;s:44:"so the serialized string is a valid constant";}'); print_r(unserialize(MyConst)); Quote Link to comment https://forums.phpfreaks.com/topic/162223-solved-declaring-array-constant/#findComment-866267 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.