ChadNomad Posted October 18, 2010 Share Posted October 18, 2010 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 More sharing options...
trq Posted October 19, 2010 Share Posted October 19, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.