【问题标题】:How do I use the SplClassLoader如何使用 SplClassLoader
【发布时间】:2012-12-08 16:58:33
【问题描述】:

我正在尝试了解和使用 PSR-0 Autoloader。但它不起作用。

我的文件夹结构:

核心/

  • 图书馆/
  • 型号/

我在根目录下的 index.php

<?php
require_once 'Core/Library/SplClassLoader.php';

$loader = new SplClassLoader('Core', 'Core');
$loader->register();



use Model\Post;

模型文件夹中的 Post.php。

<?php
namespace Model;

class Post implements PostInterface
{
// ...

后接口

<?php
namespace Model;

interface PostInterface
{
//...

我得到以下错误:

致命错误:在第 17 行的 C:\wamp\www\Test\index.php 中找不到类“Model\Post”

Line 17: Init new Post;

我在这里做错了什么?

【问题讨论】:

    标签: php psr-0


    【解决方案1】:

    SplClassLoader 的工作方式如下:

    $loader = new SplClassLoader('NamespaceName', 'path/To/Base/Directory');
    

    你在这里注册Core命名空间,但是你没有Core\Model命名空间,而只是Model

    【讨论】:

    • 你是对的,但是,现在我终于找到了“Post 模型”,但是,我在找到 PostInterface 时遇到了问题,致命错误:在 C:\ 中找不到接口“PostInterface” wamp\www\Test\Core\Model\Post.php
    • PostInterface.php 位于您的Core/Model 目录中?
    • 是的,我也错过了 PostInterface 中的 Core\ 部分
    猜你喜欢
    • 2013-01-13
    • 2012-03-14
    • 2013-06-20
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    相关资源
    最近更新 更多