PHP Chaos

Modern PHP: namespace's prac

2017-01-03  本文已影响5人  xiaojianxu

--- 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;
上一篇下一篇

猜你喜欢

热点阅读