Should class method code directly access external variables?

I have specific PHP class methods that access external variables. These variables are not passed as arguments, but rather are used by code in methods.

One method uses the DEFINEd variable in the configuration file and its purpose should be available for every part of the application that needs it. It seems good to me.

Another method directly accesses the variable $ _GET var. There is code that handles the case when this var is not installed, but for some reason it smells to me.

Are these two cases something I should do, or should I have a strict pass-as-arguments statement in place?

0
source share
2 answers

Since you are already working with PHP classes and have the ability to change the architecture, you should try to separate them as much as possible from external variables. If you need to do backward compatibility, you can also set default variables.

class Bla
{
    public function blaBla($var = false)
    {
        if(!$var && isset($_GET))
            $var = $_GET;
        // ...
    }
}

$bla = new Bla();
$bla->blaBla();
$bla->blaBla($_GET);

define() ( , Zend Framework). , . . , define() , , "" ( , ... ).

+1

(http) API .

+1

All Articles