Dhanu K
Dhanu K

Reputation: 12403

conditionally define constants in php

In CodeIgniter, Is it possible to define constants globally with the session checking as in this below example

if($this->session->userdata("private")){
    define("TAB_ACCOUNTS","accounts");
    define("TAB_INVENTORY","inventory");
    define("TAB_INVENTORY_PRODUCTS","inventory_products");
    define("TAB_INVENTORY_VIEW","inventory_view");
    define("TAB_ORDER_PRODUCTS","order_products");
    define("TAB_ORDERS","orders");
    define("TAB_ORDERS_VIEW","orders_view");
    define("TAB_VENDORACCOUNTS","vendoraccounts");
}else{
    define("TAB_ACCOUNTS","back_accounts");
    define("TAB_INVENTORY","back_inventory");
    define("TAB_INVENTORY_PRODUCTS","back_inventory_products");
    define("TAB_INVENTORY_VIEW","back_inventory_view");
    define("TAB_ORDER_PRODUCTS","back_order_products");
    define("TAB_ORDERS","back_orders");
    define("TAB_ORDERS_VIEW","back_orders_view");
    define("TAB_VENDORACCOUNTS","back_vendoraccounts");
}

My Requirement is to use table name MySQL queries based on the session...

UPDATE: I need to declare those constants in config.php or anywhere. I need to access those throughout the project not in a function or in a controller

Upvotes: 0

Views: 1100

Answers (2)

DFriend
DFriend

Reputation: 8964

Because you need to have a session up and running the easiest solution is to utilize a 'post_controller_constructor' hook.

In config.php

$config['enable_hooks'] = TRUE;

The file application/config/hooks.php

$hook['post_controller_constructor'][] = array(
  'class' => '',
  'function' => 'set_tab_constants',
  'filename' => 'post_controller_hook.php',
  'filepath' => 'hooks'
);

In the file application/hooks/post_controller_hook.php

function set_tab_constants()
{
    if(isset($_SESSION['private']))
    {
        define("TAB_ACCOUNTS", "accounts");
        define("TAB_INVENTORY", "inventory");
        define("TAB_INVENTORY_PRODUCTS", "inventory_products");
        define("TAB_INVENTORY_VIEW", "inventory_view");
        define("TAB_ORDER_PRODUCTS", "order_products");
        define("TAB_ORDERS", "orders");
        define("TAB_ORDERS_VIEW", "orders_view");
        define("TAB_VENDORACCOUNTS", "vendoraccounts");
    }
    else
    {
        define("TAB_ACCOUNTS", "back_accounts");
        define("TAB_INVENTORY", "back_inventory");
        define("TAB_INVENTORY_PRODUCTS", "back_inventory_products");
        define("TAB_INVENTORY_VIEW", "back_inventory_view");
        define("TAB_ORDER_PRODUCTS", "back_order_products");
        define("TAB_ORDERS", "back_orders");
        define("TAB_ORDERS_VIEW", "back_orders_view");
        define("TAB_VENDORACCOUNTS", "back_vendoraccounts");
    }
}

Directly accessing the superglobal $_SESSION avoids the overhead of getting an instance of CI so that the session class can be used to read userdata. That method directly reads the $_SESSION variable anyway. Avoid the middle-man and buy direct!

In PHP the scope of a constant is global. You can access constants anywhere in your script without regard to scope.

Upvotes: 2

Fernando Mertins
Fernando Mertins

Reputation: 186

as far as I can see, your public/backend tables (the else block) are always prefixed with back_, so I would recommend not using these constants.

Instead, use the normal table name in your models and create a single method that verifies the session and returns an empty string or the "back_" string, and then use this method in your "from" clause.

In other words: " why having a lot of constants when the only difference is a string prefix?"


Plus, be careful with your requisite if these mirrored tables represent the same records, for instance, if records at accounts table are the same kind/type of the records in back_accounts I understand they should be all the same table. :-) I'm supposing you can't change this requisite :-)

Upvotes: 0

Related Questions