You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP使用hQuery.php库遇类未找到/类已存在错误求助

Troubleshooting hQuery.php: Fixing "Class Not Found" and "Class Already In Use" Errors

Hey there! Let's walk through what's causing these two common errors with hQuery.php and how to fix them step by step.

First Error: "Class not found"

This usually means PHP can't locate the hQuery class files. Here are the most likely issues and fixes:

  • Incorrect file path when including the library
    If you're manually loading hQuery.php, double-check your require/require_once path. For example, if the library sits in a vendor/duzun/hQuery.php/src folder, use:

    require_once __DIR__ . '/vendor/duzun/hQuery.php/src/hQuery.php';
    

    You can verify the path is valid with file_exists(__DIR__ . '/path/to/hQuery.php') if you're unsure.

  • Composer autoloader not set up
    If you installed hQuery via Composer, make sure you're loading the autoloader first (and skip manual includes entirely):

    require_once __DIR__ . '/vendor/autoload.php';
    

    If you haven't run composer require duzun/hquery.php yet, execute that command first to install the library properly.

  • File permissions or include_path issues
    Ensure the web server/PHP process has read access to the hQuery files. You can also check your PHP include_path with echo get_include_path(); to confirm the library's directory is listed.

Second Error: "Cannot declare class hQuery_Context, because the name is already in use"

This error pops up when the same class gets loaded multiple times. Here's how to resolve it:

  • Accidental duplicate includes
    Replace any require statements with require_once (or include_once). This guarantees the hQuery files are only loaded once, even if the include line runs multiple times in your code.

  • Mixing Composer autoloader and manual includes
    If you're using Composer's autoloader, don't manually require hQuery.php again. The autoloader handles loading the class when it's first used, so adding a manual include will trigger a duplicate load conflict.

  • Rare class name conflict
    Search your project for any other files that define a hQuery_Context class. If you find one, you'll need to rename that conflicting class or adjust your loading order to avoid the clash.

Working Example

Here's a simple, correct implementation using Composer:

<?php
// Enable error reporting for debugging (optional but helpful)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Load Composer autoloader
require_once __DIR__ . '/vendor/autoload.php';

// Use hQuery to fetch and parse a page
$document = hQuery::fromUrl('https://example.com');
$pageTitle = $document->find('title')->text();
echo "Page Title: " . $pageTitle;

If you're not using Composer, here's a manual setup example:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Load hQuery once with correct path
require_once __DIR__ . '/path/to/hQuery.php';

// Parse a local HTML string
$html = '<div class="content"><p>Hello hQuery!</p></div>';
$document = hQuery::fromString($html);
echo $document->find('.content p')->text();

内容的提问来源于stack exchange,提问作者pioupiou1211

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:36:25