NotionCommotion Posted November 16, 2014 Share Posted November 16, 2014 Often in the very beginning of index.php, I will define a bunch of constants. To make sure I can quickly identify them as being one of my defined constants, I will often include some sort of prefix. define ('xzy_somecontant',123); define ('xzy_anothercontant',321); Sometimes I have a bunch of constants that are related; define ('xzy_id_for_page1',123); define ('xzy_id_for_page2',231); define ('xzy_id_for_page3',312); It would be nice to somehow group them into say "xzy_page_ids", and then access them by some index such as "page1" or "1" (or whatever makes sense for the given naming structure). Is this possible? Is there another defacto way of doing so such as a static class or something? Thanks Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 16, 2014 Share Posted November 16, 2014 the constant() function that will let you dynamically get the value of any constant given a string containing the constant name, which you can dynamically produce. however, it sounds like you need to use an array to hold your related values (the actual values could be defined constants), where you can use the array index to access the correct element/entry. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 16, 2014 Author Share Posted November 16, 2014 Thanks mac_gyver. I've never used constant() before. Looks like it is only needed when you wish to dynamically assemble the constant name (No point in using echo constant("MAXSIZE"); as described by http://php.net/manual/en/function.constant.php, right?). The array approach would work, however, I would need to use a global variable if used in multiple scripts and there is no way to prevent them from inadvertently being changes, so they are not true constants, right? Maybe a singleton class which is accessed as const::const('someConstant') or const::const('someGroupConstant','someGroup')? Or is what I am asking not typically desired, and maybe I should re-think my needs? Quote Link to comment Share on other sites More sharing options...
ignace Posted November 16, 2014 Share Posted November 16, 2014 Instead of storing ID's in constants, maybe you can use OOP to accomplish what you are doing: class PageRepository { private static $QUERY_HOME_PAGE = 'SELECT .. FROM pages WHERE page_id = 1'; private static $QUERY_CONTACT_PAGE = 'SELECT ..'; private $pdo; private $pageFactory; public function __construct(PDO $db, PageFactory $pageFactory) { $this->pdo = $db; $this->pageFactory = $pageFactory; } public function getHomePage() { $stmt = $this->pdo->query($this->QUERY_HOMEPAGE); // return $this->pageFactory->createFromArray($data); } public function getContactPage() { return $this->pageFactory->createFromArray(..); } } Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 16, 2014 Author Share Posted November 16, 2014 Instead of storing ID's in constants, maybe you can use OOP to accomplish what you are doing: Thanks ignace, Yes, I was thinking of something similar. May I ask whether you actually do so in practice? What is the purpose of type hinting (PDO and PageFactory) the arguments sent to __construct()? Quote Link to comment 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.