如何使用名称空间包含PHP类
我还没有完全了解PHP中的名称空间。我也不使用作曲家或自动加载器。我理解它们,但通常很难将它们纳入我自己的项目中 所以我想在我正在开发的Wordpress插件中包含一个包,特别是这个 我可以包含主文件OK,但在该文件中调用函数时会出现以下错误。我不确定这是否与它使用名称空间有关,或者仅仅是因为它试图在Api子文件夹中包含文件,而一旦我将主文件包含到自己的代码中,它就不是正确的路径 有人知道我如何在我自己的项目中使用这个包吗如何使用名称空间包含PHP类,php,namespaces,Php,Namespaces,我还没有完全了解PHP中的名称空间。我也不使用作曲家或自动加载器。我理解它们,但通常很难将它们纳入我自己的项目中 所以我想在我正在开发的Wordpress插件中包含一个包,特别是这个 我可以包含主文件OK,但在该文件中调用函数时会出现以下错误。我不确定这是否与它使用名称空间有关,或者仅仅是因为它试图在Api子文件夹中包含文件,而一旦我将主文件包含到自己的代码中,它就不是正确的路径 有人知道我如何在我自己的项目中使用这个包吗 require_once('Thinkific/Thinkific.ph
require_once('Thinkific/Thinkific.php');
$think = new \Thinkific\Thinkific([
'apikey' => 'xxxxxxxxx',
'subdomain' => 'yyyyyyyyy',
'debug' => true
]);
$users = $think->users();
$users = $users->getAll();
但这是一个错误,它表明类文件和Api子文件夹中的类未加载
Fatal error: Uncaught Error: Class '\Thinkific\Api\Users' not found in Fatal error: Uncaught Error: Class '\Thinkific\Api\Users' not found in /mysite/public_html/wp-content/plugins/thinkific/Thinkific/Thinkific.php:51 Stack trace: #0 /mysite/public_html/wp-content/plugins/thinkific/Thinkific/Thinkific.php(36): Thinkific\Thinkific->getApi('\\Thinkific\\Api\\...') #1 /mysite/public_html/wp-content/plugins/thinkific/thinkific.php(50): Thinkific\Thinkific->__call('users', Array) #2 /mysite/public_html/wp-content/plugins/thinkific/thinkific.php(29): Thinkific::thinkific_get_users() #3 /mysite/public_html/wp-includes/class-wp-hook.php(298): thinkific_woocommerce_order_status_completed(Object(WP)) #4 /mysite/public_html/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters('', Array) #5 /mysite/public_html/wp-includes/plugin.php(515): WP_Hook->do_action(Array) #6 /mysite/public_html/wp-includes/class-wp.php(746): do_action_ref_array('wp in /mysite/public_html/wp-content/plugins/thinkific/Thinkific/Thinkific.php on line 51
试着这样做: lib.php:
<?php
// Application library 1
namespace App\Lib1;
const MYCONST = 'Hello,';
// Application library 1
namespace App\Lib2;
const MYCONST = 'How are you?';
function MyFunction() {
return __FUNCTION__;
}
class MyClass {
static function WhoAmI() {
return __METHOD__;
}
}
?>
myapp.php:
<?php
require_once('lib.php');
echo App\Lib1\MYCONST . "\n";
echo App\Lib2\MYCONST . "\n\n";
echo App\Lib2\MyFunction() . "\n";
echo App\Lib2\MyClass::WhoAmI() . "\n";
?>
您使用的库依赖于
composer
,您在那里安装composer
它将生成一个vendor/autoload.php
,您只需在这里生成vendor/autoload.php
,这个文件将负责自动加载类
require_once 'vendor/autoload.php';
$think = new \Thinkific\Thinkific([
'apikey' => 'xxxxxxxxx',
'subdomain' => 'yyyyyyyyy',
'debug' => true
]);
$users = $think->users();
$users = $users->getAll();
谢谢,但我不用作曲家。