gpt4 book ai didi

php - 在 php : class not found 中使用库

转载 作者:行者123 更新时间:2023-12-04 16:59:08 25 4
gpt4 key购买 nike

我正在尝试使用这个库:https://github.com/duzun/hQuery.php

我的项目是这样的:

BetCompare
Application
Teams
file_using_the_library.php
hQueryLib
hquery.php

所以这是我在我的 php 文件中使用它的方式:
namespace BetCompare\Application\Teams;
use BetCompare\Application\Teams\hQueryLib\hquery;

hQuery::$cache_path = "/path/to/cache";

这将返回错误 Class not found .在对此事进行了一些研究后,我尝试了这个:
namespace BetCompare\Application\Teams;
use BetCompare\Application\Teams\hQueryLib\hquery;
include_once 'hQueryLib/hquery.php';

hQuery::$cache_path = "/path/to/cache";

那么错误如下: Cannot declare class hQuery_Context, because the name is already in use .我不明白,第二个错误使它看起来像 use就足够了并加载了类(class)。但我不能使用它......我做错了什么?

我也试过只使用 include_once但它不起作用。

最佳答案

要么像这样(使用命名空间 duzun\hQuery ):

<?php

namespace BetCompare\Application\Teams;

use duzun\hQuery;

include_once 'hQueryLib/hquery.php';
hQuery::$cache_path = "/path/to/cache";

或者像这样,没有命名空间:
<?php

namespace BetCompare\Application\Teams;

include_once 'hQueryLib/hquery.php';
\hQuery::$cache_path = "/path/to/cache";

你的代码不能工作,因为如果你写
use BetCompare\Application\Teams\hQueryLib\hQuery;

您实际上是在假设 hQuery类具有命名空间定义
namespace BetCompare\Application\Teams\hQueryLib;

它没有(作为第三方类)。

带有 duzun\hQuery 的部分定义在 hQuery.php 的最后几行文件并在文档中进行了描述。

关于php - 在 php : class not found 中使用库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48834833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com