OOP for two interacting classes

What are the relative advantages / disadvantages of chaining (or rather, using the results of one class to generate another) compared to nesting classes?

I am trying to restructure a user / authentication system and I wonder

  • myAuthClass should act as a utility, and if the login is successful, just create a new myUserClass object
  • or should myAuthClass create an internal myUserClass object (i.e. $ this-> user = new myUserClass)
  • or even if myUserClass should just call myAuthClass if necessary (i.e. when the user tries to log in) and update its internal structure (new emails, favorites, cart, etc.) as necessary.

As you can tell, I'm a little OOP n00b. Specifically, I seem to be able to do the work for each of the methods, so I am interested in hearing from other people regarding + ves / -ves different approaches.

Greetings.

+3
source share
3 answers

In an academic perspective, all 3 options are incorrect, since they are programmed for specific implementations, and not for an abstract interface. Thus, they are directly related to each other, which limits reuse - you reuse them both or not at all.

IUserClass IAuthClass , , auth- IUserClass ​​ IAuthClass .

, auth , UserClass , IAuthClass.

( IAuthClass) , , . , .

, .

+1

, . , ( ), , . , / , LADP ..

, , . Zend_Auth, . Zend_Auth, , , , .

, Zend_Auth ;

        protected function Authentication() {
        $strEmail = trim($this->txtEmail->Text);
        $this->txtPassword->Text = trim($this->txtPassword->Text);
        // do the process of authentication, AD and then DB
        // Get a reference to the singleton instance of QAuth
        $auth = QAuth::getInstance();
        // Set up the authentication adapter
        $authAdapter = new QAuth_Adapter_WebService(__LOGIN_WS_URL__, 
            $strEmail, $this->txtPassword->Text
        );

        // Attempt authentication, saving the result
        $result = $auth->authenticate($authAdapter);

        if ($result->isValid()) {
            $objUser = User::LoadByEmail($strEmail);

            // if there is not a user record create one
            if(!$objUser) {
                $this->User_Create($strEmail);
                $objUser = User::LoadByEmail($strEmail);
            }

            $crypt = new Encryption();
            $encr = $crypt->encrypt(__KEY__, $objUser->UserID);             
            $_SESSION['user_id'] = $encr;
            setcookie('user_id', $_SESSION['user_id'], time()+(3600*24*365*20));
            $this->Intranet1Integration($objUser->UserID);
            QApplication::Redirect('http://'.__URL__.'/index.php');     
        }
        else {
            QApplication::DisplayAlert(
                'Log on failed. You must provide a Company email and a correct password.'
            );
        }
    }
+1

, ( ) . , , , .

, .

0

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


All Articles