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
https://forums.phpfreaks.com/topic/216200-oop-and-constants/
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
https://forums.phpfreaks.com/topic/216200-oop-and-constants/#findComment-1123715
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.