简体   繁体   English

PSR-0找不到的班级

[英]Class not found with PSR-0

I'm completely stuck on this issue! 我完全坚持这个问题! I'm using the php-simple-html-dom-parser and this is loaded with PSR-0. 我正在使用php-simple-html-dom-parser ,它加载了PSR-0。 I'm not that familiar with PSR-0. 我对PSR-0并不熟悉。 But I use the class just as the readme file shows but I can't get it working. 但我正如自述文件所示使用该类,但我无法使其工作。

In my IDE, autcompletion is working fine and not any error is shown. 在我的IDE中,autcompletion工作正常,并没有显示任何错误。 But when I run the script, I get this error: 但是当我运行脚本时,我收到此错误:

Fatal error: Class 'Sunra\\PhpSimple\\HtmlDomParser' not found in C:\\xampp\\htdocs\\folder\\test.php on line 3 致命错误:第3行的C:\\ xampp \\ htdocs \\ folder \\ test.php中找不到类'Sunra \\ PhpSimple \\ HtmlDomParser'

Code: 码:

<?php
use Sunra\PhpSimple\HtmlDomParser;
$dom = HtmlDomParser::file_get_html();

File structure: 文件结构:

folder\
  Sunra\
    PhpSimple\
      simplehtmldom_1_5\
      HtmlDomParser.php
test.php

As I said, I really have no idea what is going wrong. 正如我所说,我真的不知道出了什么问题。 So probably I'm looking for the wrong thing and that's why I post this question. 所以我可能正在寻找错误的东西,这就是我发布这个问题的原因。

Download Composer 下载Composer

In your project directory run: 在项目目录中运行:

php composer.phar require sunra/php-simple-html-dom-parser ~1.5

In your php file (index.php for example), add: 在你的php文件中(例如index.php),添加:

<?php

include 'vendor/autoload.php';

use Sunra\PhpSimple\HtmlDomParser;

$dom = HtmlDomParser::file_get_html();

Here, when you include vendor/autoload.php the composer generated autoloading configuration handles all class autoloads for you. 在这里,当您包含vendor/autoload.php ,作曲家生成的自动加载配置会为您处理所有类自动加载。 Nearly all libraries on github that follow PSR-0 exist on Packagist making them available for install in this way. 几乎所有跟随PSR-0的github上的库都存在于Packagist上,使得它们可以通过这种方式安装。

You may want to read up on Semantic Versioning to get an idea of what you are installing when you require libraries. 您可能希望阅读语义版本控制,以了解在需要库时要安装的内容。

Most modern PHP frameworks now use Composer for their dependencies. 大多数现代PHP框架现在都使用Composer来实现它们的依赖关系。 However it is still fully feasible for small projects of even a single file. 但是,即使是单个文件的小项目,它仍然完全可行。

Well, you aren't using an autoloader , so the class will not be loaded automatically. 好吧,您没有使用自动加载器 ,因此不会自动加载该类。

Here's a minimal autoloader example that uses PSR-0. 这是一个使用PSR-0的最小自动加载器示例


After you understand Autoloading, you should try and use dependency management frameworks like Composer . 了解自动加载后,您应该尝试使用Composer之类的依赖关系管理框架。 See Flosculus's answer for more details. 有关详细信息,请参阅Flosculus的答案。

Using the PSR-0 autoloader, did you add it to a spl_autoload_register('autoload'), or are you using composer and included composers autoloader? 使用PSR-0自动加载器,您是否将它添加到spl_autoload_register('autoload'),或者您使用的是作曲家和包含的作曲家自动加载器?

This works for me: 这对我有用:

<?php
function autoload($className)
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName;
}
spl_autoload_register('autoload');

use Sunra\PhpSimple\HtmlDomParser;

$dom = HtmlDomParser::file_get_html('http://google.com');
var_dump($dom);
?>

Source: http://www.php-fig.org/psr/psr-0/ 资料来源: http//www.php-fig.org/psr/psr-0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM