Jump to content

Grouping constants


NotionCommotion

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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(..);
  }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.