I wanted to integrate my app with google callendar.
I added google PHP API to libraries/Google
.
libraries/Google/autoload.php
is:
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);
}
}
);
Output is:
/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
Output is:
/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 seems to work only when it wants. Or is there any magic I do not know about?
PHP Version 5.4.36-0+tld0
Tried Class "Google_Config" not found
and Spl_autoload_register() not working on server
Edit: BASE_P
is defined as: dirname(__FILE__).'/'
in main dir.
Edit2: Tried including classes manually. Autoloader stops working always after: libraries/Google/Model.php
is loaded. But if I load Model.php before everything else (right after registering autoloader) it does not seem to break autoloader. But it still stops after few autoloads.
It's an old project, and I finally found a culprit:
function __autoload($class_name) {
$dir = dirname(__FILE__).'/';
if (file_exists($dir.'classes/'. $class_name . '.php'))
require_once $dir.'classes/'. $class_name . '.php';
}
Changing it to:
spl_autoload_register(
function ($class_name) {
$dir = dirname(__FILE__).'/';
if (file_exists($dir.'classes/'. $class_name . '.php'))
require_once $dir.'classes/'. $class_name . '.php';
);
Fixed the other autoloader.
Moral? Never mix __autoload() and spl_autoload_register()
Basically never use __autoload() as it is deprecated and like this example shows, it can break things.
(or will probably be as stated http://php.net/manual/en/language.oop5.autoload.php)
Hope it will help someone someday.