Do you consider bad form in PHP to access super-smoothed class methods?

Take an example login()function in class Account.

class Account {
 /* Class variables */

    public function login() {
        if(isset($_POST['username']) && isset($_POST['password']))
            return $this->_formLogin();
        else if(isset($_SESSION['accountId']))
            return $this->_sessionLogin();
        else if(isset($_COOKIE['username']) && isset($_COOKIE['password']))
            return $this->_cookieLogin();
        else return false;
    }

    private function _formLogin() {
        //perform login actions using $_POST data
    }
    /* All that other stuff */
}

Try at this point to ignore any concerns about invisible methods for disinfecting data, salting passwords, etc. Focusing strictly on login(), is this global access bad juju? I avoid using PHP super globals inside classes normally, but I can't think of a good reason not to do this in this situation.

I can understand why you do not want magic in the background to occur when global classes interact with classes, but these global variables are built into PHP, are not modified by the class, and are used only by this class.

, , :

$user = new Account($whatever, $objects, $we, $depend, $on);
if($user->login()) {
    //Do this stuff when logged in
}

, , , :

$user = new Account($whatever, $objects, $we, $depend, $on);
if(isset($_POST['username']) && isset($_POST['password']))
    $user->formLogin($_POST['username'], $_POST['password']);
else if(isset($_SESSION['accountId']))
    $user->sessionLogin($_SESSION['accountId']);
else if(isset($_COOKIE['username']) && isset($_COOKIE['password']))
    $user->cookieLogin($_COOKIE['username'], $_COOKIE['password']);
if($user->isLoggedIn() {
    //Do this stuff when logged in
}

, , ?

+3
4

, "" "" . ( $_GET $_POST $_SESSION) , , , , .

, , - - ( ) . .

, .

+7

, , , -, . - , - - , PHP .

- - . , .

+7

- . , Zend Framework , . Cookies.

+2

I would say it depends on your application design. If a class is some common, loosely coupled class or module, and suddenly it uses such global vars, that would be bad practice in my book. But if the class is clearly focused on a specific task, for which all these global vars are needed (for example, your login), I do not see any obvious objections.

0
source

Source: https://habr.com/ru/post/1706304/


All Articles