Jump to content

OOP and Constants?


ChadNomad

Recommended Posts

Hey all,

 

Another OOP question from me :)

 

Is using constants in a class a bad idea? For example I have a config file and have this in a class:

header("Location: ".LOGIN_LOCATION);

 

Also is it OK to define $_SERVER variables in a construct? For example I have:

class whatever
{
    private $_remote_addr;
    private $_user_agent;
    
    function __construct()
    {
        $this->_remote_addr     = $_SERVER['REMOTE_ADDR'];
        $this->_user_agent     = $_SERVER['HTTP_USER_AGENT'];
    }

 

.. or is it OK just to use $_SERVER for example in a mysql query?

 

Thanks for all the help so far. Getting there!

Link to comment
Share on other sites

Is using constants in a class a bad idea? For example I have a config file and have this in a class:

header("Location: ".LOGIN_LOCATION);

 

In general, methods should return a simple value (bool, array, exception). Your client code should then be responsible for executing any logic based on these results.

 

Also is it OK to define $_SERVER variables in a construct? For example I have:

class whatever
{
    private $_remote_addr;
    private $_user_agent;
    
    function __construct()
    {
        $this->_remote_addr     = $_SERVER['REMOTE_ADDR'];
        $this->_user_agent     = $_SERVER['HTTP_USER_AGENT'];
    }

 

.. or is it OK just to use $_SERVER for example in a mysql query?

 

Thanks for all the help so far. Getting there!

 

Using globals within classes / functions is never a good idea.

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.