PHP中的魔術方法

來源:互聯網
上載者:User

PHPmso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:
Calibri;mso-hansi-theme-font:minor-latin">中有下列稱之為魔術方法(magic method)mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:
Calibri;mso-hansi-theme-font:minor-latin">的函數:__construct, __destruct ,
__call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup,
__toString, __set_state, __clone and __autoload,mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:
Calibri;mso-hansi-theme-font:minor-latin">本文使用__call宋體;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin">為實現一個身分識別驗證的簡單一實例,代碼如下:

 代碼

<?php
    interface Accountable
    {
        const ERR_MSG = "error";
        public function isLoggedIn();
        public function getAccount($user = '');
    }
    abstract class Authentication implements Accountable
    {
        private $account = null;
        public function getAccount($user = '')
        {
            if ($this->account != null) {
                return $this->account;
            } else {
                return ERR_MSG;
            }
        }
        public function isLoggedIn()
        {
            return ($this->account != null);
        }
    }
    class Users
    {
        private static $accounts = array('phinecos' => 'phine',
                                         'guest'    => 'guest'
                                         );
        public static function validates($user, $passwd)
        {
            return self::$accounts[$user] == $passwd;
        }
        public function __call($name, array $arguments)
        {
            if (preg_match("/^validates(.*)$/", $name, $matches) && count($arguments) > 0) {
                return self::validates($matches[1], $arguments[0]);
            }
        }
    }
    class MyAuth extends Authentication
    {
        private $users;
        public function __construct()
        {
            $this->users = new Users();
        }
        public function login($user, $passwd)
        {
            if (empty($user) || empty($passwd)) return false;
            $firstValidation = Users::validates($user, $passwd);
            $userFunction = 'validates'.$user;
            $secondValidation = $this->users->$userFunction($passwd);
            return ($firstValidation && $secondValidation);
        }
    }
    function main()
    {
        $authenticator = new MyAuth();
        $user = 'phinecos';
        $pwd = 'phine';
        $isValid = $authenticator->login($user, $pwd);
        if ($isValid) {
            echo 'valid user';
        } else {
            echo 'invalid user';
        }
    }
    main();
?>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.