美文网首页PHP Chaos
Modern PHP: namespace's prac

Modern PHP: namespace's prac

作者: xiaojianxu | 来源:发表于2017-01-03 19:20 被阅读5次

    --- mysql_trait.php ---

    <?php
    namespace james;
    
    trait db_connect
    {
    
        protected $host = 'bbbb';
        protected $user;
        protected $pwd;
        protected $db;
        
        protected $connect;
        
        public function __construct($host, $user, $pwd, $db)
        {
            $this->host = $host;
            $this->user = $user;
            $this->pwd = $pwd;
            $this->db = $db;
        }
        
        public function connect()
        {
            $this->connect = mysqli_connect($this->host, $this->user, $this->pwd);
            mysqli_select_db($this->connect, $this->db);
        }
        
        public function query($table, $arr_fields = array(), $order_by = false)
        {
            
            $sql = 'SElECT * FROM {$table}';
            
            return mysqli_query($this->connect, $sql);
        }
    }
    
    

    --- News.php ---

    <?php
    namespace james;
    include_once 'mysql_trait.php';
    
        class News
        {
        
            use db_connect;
            
            public function __construct($host, $user, $pwd, $db)
            {
                $this->host = $host;
                //var_dump($this->host);
                //$db = new m
                //echo 232323;
            }
            
            
            public function latestNews()
            {
                
            }
        
        }
    

    --- test.php ---

    <?php
    
    namespace ModernPHP;
    include_once 'News.php';
    
    use james\News;
    
    class test {
    
        public function __construct()
        {
            $news_object = new News('localhost', 'root', '', 'invo');
    
            print_r($news_object);
        }
    }
    
    $test = new Test;
    

    相关文章

      网友评论

        本文标题:Modern PHP: namespace's prac

        本文链接:https://www.haomeiwen.com/subject/hqunvttx.html