Modern PHP: namespace's practice

--- 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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • php+mysql+apache+centos 编译安装 领导要求先保证php环境稳定,然后再去考虑其他,例如性能...
    dnaEMx阅读 2,238评论 1 16
  • PHP 学习目录 ├─PHP视频教程 1 LAMP网站构建 │ ├─PHP教程 1.1.1 新版视频形式介绍│ ...
    曹渊说创业阅读 16,175评论 29 417
  • 更改ip和dnsVi /etc/sysconfig/network-scripts/ifcfg-eth0vi /...
    Xwei_阅读 1,853评论 0 3
  • 时光悠悠,岁月无痕,听一首老歌,念一个旧人,直到窗外的天黑了又白,白了又黑,那棵摇曳的梧桐莎莎在晚秋的风中。 过了...
    三月烟霞阅读 256评论 1 0
  • 发现自己上一次登录还在去年十月份,为啥之后很少写了? 回忆了一下,原来第一是手机被偷,再后来买了新手机没装简书的A...
    林恩阅读 397评论 0 1