返回模拟用户登录验......登陆

模拟用户登录验证的案例 依赖注入 Facade

肖凌2019-10-31 22:56:2636

//virefy.php

<?php

 //数据库连接类

class Db{
            public $databaseType = null;

             //获得数据库类型

             public function getType($databaseType){
                            $this->databaseType = $databaseType;
                            return $this;

             }

            //验证数据库类型

             public function conn(){

                             if($this->databaseType == 'mysql'){
                                        return '数据库连接成功';
                            }else{
                                        return '数据库连接失败';
                            }

             }
        }
        //数据库验证类

        class Validate{
                     protected $username;
                     protected $password;

                     //获取数据库用户名和密码

                     public function getValid($username,$password){
                                            $this->username = $username;
                                            $this->password = $password;
                                        return $this;

                     }

                     //验证数据库用户名和密码

                     public function check(){
                                    if($this->username == 'root' && $this->password == 'root'){
                                                return '验证成功'; }else{ return '验证失败';

                                     }

                     }

         }
        //数据显示类

        class User{
                     protected $username;
                     protected $password;

                     //获取用户名和密码

                     public function getUser($username,$password){
                                    $this->username = $username;
                                    $this->password = $password;
                                    return $this;

                     }

                     //验证登录用户名和密码

                     public function login(){

                                     if($this->username == 'admin' && $this->password == 'admin'){

                                             return '用户登录成功';

                                      }else{

                                             return '用户登录失败';

                                     }

                 }
        }


//container.php

<?php

        require 'virefy.php';
        class Container{

             //声明对象数组

             public $instance = [];
            //绑定类

             public function bind($classType,closure $closeFun){
                        $this->instance[$classType] = $closeFun;
            }
            //调用容器绑定类方法

             public function make($classType,$params = []){

                //通过数组赋值的匿名函数返回类对象

                         return call_user_func_array($this->instance[$classType],$params);
            }

     }
        //声时一个容器实例

        $container = new Container;

        //绑定Db

        $container->bind('Db',function(){ return new Db; });

        //绑定Validate

        $container->bind('Validate',function(){ return new Validate; });

        //绑定User $container->bind('User',function(){ return new User; });

        <?php

                require 'container.php';
                class Facade{

                             //声明容器类变量

                             public static $container = null;

                             //构造容器类赋值

                             public static function init(Container $container){
                                            self::$container = $container;

                             }
                            public static function conn($databaseType){
                            //返回Db类conn方法

                             return self::$container->make('Db')->getType($databaseType)->conn();

                            }
                            public static function check($username,$password){
                            //返回Validate类check方法

                             return self::$container->make('Validate')->getValid($username,$password)->check(); }
                            public static function login($username,$password){
                            //返回User类display方法

                             return self::$container->make('User')->getUser($username,$password)->login();

                             }
        }
    //Facade类初始化容器

        Facade::init($container);

    //Facade类验证数据库类型

        echo Facade::conn('mysql').'<br>';

    //Facade类验证数据库用户和密码

        echo Facade::check('root','root').'<br>';

    //Facade类验证登录用户和密码

        echo Facade::login('admin','admin').'<br>';

最新手记推荐

• 依赖注入小例子• 模板显示用户信息表,并分页显示• 模板继承,模仿php页面• 模拟用户登录验证的案例 依赖注入 Facade• 创建一个验证器, 用来对模型对应的表中字段进行验证处理

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • PHP中文网