PHP spl_autoload_register 在某些位置没有被调用 Google API PHP
PHP spl_autoload_register not being called in certain locations Google API PHP
我想将我的应用程序与 google callendar 集成。
我添加了 google PHP API 到 libraries/Google
.
libraries/Google/autoload.php
是:
define ('GOOGLE_LIB_PATH', BASE_P . 'libraries/Google/');
set_include_path(
get_include_path() . PATH_SEPARATOR . GOOGLE_LIB_PATH
);
spl_autoload_register(
function ($className) {
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
// Drop 'Google', and maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 1, 2);
$filePath = GOOGLE_LIB_PATH . implode('/', $classPath) . '.php';
echo $filePath .'<br>';
if (file_exists($filePath)) {
require_once($filePath);
}
}
);
输出为:
/home/users/page/libraries/Google/Service/Calendar.php
/home/users/page/libraries/Google/Service.php
/home/users/page/libraries/Google/Service/Resource.php
/home/users/page/libraries/Google/Client.php
/home/users/page/libraries/Google/Collection.php
/home/users/page/libraries/Google/Model.php
Fatal error: Class 'Google_Config' not found in /home/users/page/libraries/Google/Client.php on line 77
添加一个调用后它开始加载配置,但在另一个调用停止
define ('GOOGLE_LIB_PATH', BASE_P . 'libraries/Google/');
set_include_path(
get_include_path() . PATH_SEPARATOR . GOOGLE_LIB_PATH
);
spl_autoload_register(
function ($className) {
// same as above
}
);
$x = new Google_Config; // Added this line
输出为:
/home/users/page/libraries/Google/Config.php
/home/users/page/libraries/Google/Service/Calendar.php
/home/users/page/libraries/Google/Service.php
/home/users/page/libraries/Google/Service/Resource.php
/home/users/page/libraries/Google/Client.php
/home/users/page/libraries/Google/Collection.php
/home/users/page/libraries/Google/Model.php
Fatal error: Class 'Google_Auth_OAuth2' not found in /home/users/page/libraries/Google/Client.php on line 614
Autoloader 似乎只在需要时工作。还是有什么我不知道的魔法?
PHP 版本 5.4.36-0+tld0
尝试过 Class "Google_Config" not found
和 Spl_autoload_register() not working on server
编辑:BASE_P
定义为:dirname(__FILE__).'/'
in main dir.
Edit2:尝试手动包含 类。自动加载器总是在加载 libraries/Google/Model.php
后停止工作。但是如果我在其他一切之前加载 Model.php (在注册自动加载器之后)它似乎不会破坏自动加载器。但在几次自动加载后它仍然停止。
老项目了,终于找到罪魁祸首了:
function __autoload($class_name) {
$dir = dirname(__FILE__).'/';
if (file_exists($dir.'classes/'. $class_name . '.php'))
require_once $dir.'classes/'. $class_name . '.php';
}
将其更改为:
spl_autoload_register(
function ($class_name) {
$dir = dirname(__FILE__).'/';
if (file_exists($dir.'classes/'. $class_name . '.php'))
require_once $dir.'classes/'. $class_name . '.php';
);
修复了另一个自动加载器。
道德?永远不要混合 __autoload() 和 spl_autoload_register()
基本上 从不 使用 __autoload() 因为它已被弃用并且像这个例子所示,它可以破坏东西。
(或者可能会如所述http://php.net/manual/en/language.oop5.autoload.php)
希望有一天它能对某人有所帮助。
我想将我的应用程序与 google callendar 集成。
我添加了 google PHP API 到 libraries/Google
.
libraries/Google/autoload.php
是:
define ('GOOGLE_LIB_PATH', BASE_P . 'libraries/Google/');
set_include_path(
get_include_path() . PATH_SEPARATOR . GOOGLE_LIB_PATH
);
spl_autoload_register(
function ($className) {
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
// Drop 'Google', and maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 1, 2);
$filePath = GOOGLE_LIB_PATH . implode('/', $classPath) . '.php';
echo $filePath .'<br>';
if (file_exists($filePath)) {
require_once($filePath);
}
}
);
输出为:
/home/users/page/libraries/Google/Service/Calendar.php
/home/users/page/libraries/Google/Service.php
/home/users/page/libraries/Google/Service/Resource.php
/home/users/page/libraries/Google/Client.php
/home/users/page/libraries/Google/Collection.php
/home/users/page/libraries/Google/Model.php
Fatal error: Class 'Google_Config' not found in /home/users/page/libraries/Google/Client.php on line 77
添加一个调用后它开始加载配置,但在另一个调用停止
define ('GOOGLE_LIB_PATH', BASE_P . 'libraries/Google/');
set_include_path(
get_include_path() . PATH_SEPARATOR . GOOGLE_LIB_PATH
);
spl_autoload_register(
function ($className) {
// same as above
}
);
$x = new Google_Config; // Added this line
输出为:
/home/users/page/libraries/Google/Config.php
/home/users/page/libraries/Google/Service/Calendar.php
/home/users/page/libraries/Google/Service.php
/home/users/page/libraries/Google/Service/Resource.php
/home/users/page/libraries/Google/Client.php
/home/users/page/libraries/Google/Collection.php
/home/users/page/libraries/Google/Model.php
Fatal error: Class 'Google_Auth_OAuth2' not found in /home/users/page/libraries/Google/Client.php on line 614
Autoloader 似乎只在需要时工作。还是有什么我不知道的魔法?
PHP 版本 5.4.36-0+tld0
尝试过 Class "Google_Config" not found
和 Spl_autoload_register() not working on server
编辑:BASE_P
定义为:dirname(__FILE__).'/'
in main dir.
Edit2:尝试手动包含 类。自动加载器总是在加载 libraries/Google/Model.php
后停止工作。但是如果我在其他一切之前加载 Model.php (在注册自动加载器之后)它似乎不会破坏自动加载器。但在几次自动加载后它仍然停止。
老项目了,终于找到罪魁祸首了:
function __autoload($class_name) {
$dir = dirname(__FILE__).'/';
if (file_exists($dir.'classes/'. $class_name . '.php'))
require_once $dir.'classes/'. $class_name . '.php';
}
将其更改为:
spl_autoload_register(
function ($class_name) {
$dir = dirname(__FILE__).'/';
if (file_exists($dir.'classes/'. $class_name . '.php'))
require_once $dir.'classes/'. $class_name . '.php';
);
修复了另一个自动加载器。
道德?永远不要混合 __autoload() 和 spl_autoload_register()
基本上 从不 使用 __autoload() 因为它已被弃用并且像这个例子所示,它可以破坏东西。
(或者可能会如所述http://php.net/manual/en/language.oop5.autoload.php)
希望有一天它能对某人有所帮助。