dourenzhou8740 2017-01-02 06:18
浏览 138

致命错误:在自定义路径中找不到命名空间类

I am trying to use PHP's traits feature and namespacing to organize my project's codes and files.

But I do not know why I get this

Fatal error: Class 'ModernPHP\News' not found in E:\www\modernphp\test.php on line 11.

There are my files in the same directory. I just have a directory modernphp; the three files are beyond this directory.

Am I misusing namespaces? Or is it something else?

mysql.trait.php

<?php
namespace ModernPHP;

trait db_connect
{

    protected $host;
    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 ModernPHP\News;

    class News
    {

        use mysql;

        /*
        public function __construct()
        {
            //$db = new m
        }
        */

        public function latestNews()
        {

        }

    }

test.php

<?php

namespace ModernPHP;

    use ModernPHP\News;

    class test {

        public function __construct()
        {
            $news_object = new News('localhost', 'root', '', 'invo');

            print_r($news_object);
        }
    }

    $test = new Test;

--- Add at Jan.06.2016 ---

class ComposerAutoloaderInita3585bdd4dd862cdaf5a9a8f6faaa488
{
    private static $loader;

    public static function loadClassLoader($class)
    {
        if ('Composer\Autoload\ClassLoader' === $class) {
            require __DIR__ . '/ClassLoader.php';
        }
    }

    public static function getLoader()
    {
        if (null !== self::$loader) {
            return self::$loader;
        }

        spl_autoload_register(array('ComposerAutoloaderInita3585bdd4dd862cdaf5a9a8f6faaa488', 'loadClassLoader'), true, true);
        self::$loader = $loader = new \Composer\Autoload\ClassLoader();
  • 写回答

2条回答 默认 最新

  • duanbohan2015 2017-01-02 06:21
    关注

    You defined your class News in namespace ModernPHP\News (see top line of News.php). That means the class, with namespace, is ModernPHP\News\News, not ModernPHP\News.

    You either meant to define News in namespace ModernPHP (replace the namespace line in News.php with namespace ModernPHP;) or you need to use ModernPHP\News\News; in your definition of ModernPHP.

    Also, you need an autoloader or explicit include/require statements to load your class and trait files. You can't simply do new News(...) if you haven't loaded the News.php file or told PHP how to do so.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测