php - spl_autoload_register not working -
i have created 5 folders containing 5 classes (ad_class, blocked_class, friend_class, image_class, profile_class) in main directory. created respective classes within mentioned folders exact name folders. i.e. if folder name ad_class class within folder same folder name in "class ad_class.
in index.php file wrote next code:
function ad_class($name) { include "ad_class/$name.php"; } function blocked_class($name) { include "blocked_class/$name.php"; } function friend_class($name) { include "friend_class/$name.php"; } function image_class($name) { include "image_class/$name.php"; } function profile_class($name) { include "profile_class/$name.php"; } spl_autoload_register("ad_class"); spl_autoload_register("blocked_class"); spl_autoload_register("friend_class"); spl_autoload_register("image_class"); spl_autoload_register("profile_class"); $a = new ad_class; $b = new blocked_class; $c = new blocked_class; $d = new image_class; $e = new profile_class;
after executing above code below warnings:
warning: include(ad_class/blocked_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4 warning: include(): failed opening 'ad_class/blocked_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4 warning: include(ad_class/image_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4 warning: include(): failed opening 'ad_class/image_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4 warning: include(blocked_class/image_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8 warning: include(): failed opening 'blocked_class/image_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8 warning: include(friend_class/image_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12 warning: include(): failed opening 'friend_class/image_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12 warning: include(ad_class/profile_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4 warning: include(): failed opening 'ad_class/profile_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4 warning: include(blocked_class/profile_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8 warning: include(): failed opening 'blocked_class/profile_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8 warning: include(friend_class/profile_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12 warning: include(): failed opening 'friend_class/profile_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12 warning: include(image_class/profile_class.php): failed open stream: no such file or directory in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 16 warning: include(): failed opening 'image_class/profile_class.php' inclusion (include_path='.;c:\users\robert\documents\web development\xampp\php\pear') in c:\users\robert\documents\web development\xampp\htdocs\xampp\web_development\development.php on line 16
why happening? can explain?
update: found next code work. if have suggestions please sense free share!
function ad_class($class) { if(!class_exists($class) && $class == "ad_class") { include "ad_class/$class" . ".php"; } elseif(!class_exists($class)) { return; } } function blocked_class($class) { if(!class_exists($class) && $class == "blocked_class") { include "blocked_class/$class" . ".php"; } elseif(!class_exists($class)) { return; } } function friend_class($class) { if(!class_exists($class) && $class == "friend_class") { include "friend_class/$class" . ".php"; } elseif(!class_exists($class)) { return; } } function image_class($class) { if(!class_exists($class) && $class == "image_class") { include "image_class/$class" . ".php"; } elseif(!class_exists($class)) { return; } } function profile_class($class) { if(!class_exists($class) && $class == "profile_class") { include "profile_class/$class" . ".php"; } elseif(!class_exists($class)) { return; } } spl_autoload_register("ad_class"); spl_autoload_register("blocked_class"); spl_autoload_register("friend_class"); spl_autoload_register("image_class"); spl_autoload_register("profile_class"); $a = new ad_class; $a->ad(); $b = new blocked_class; $b->block(); $c = new friend_class; $c->fr(); $d = new image_class; $d->image();
it produces following: advertising blocked friend image
which nil more displaying class name of instantiated object see if code worked correclty in loading classes.
spl_autoload_register()
stacks, it's going run 5 functions have defined each class seek create, regardless of class is. take illustration , rework fit needs:
spl_autoload_register( function($name) { if (is_file('/classes/'.$name.'.class.php')) { require_once('/classes/'.$name.'.class.php'); } });
checking file is_file()
save headache of no such file or directory warnings. depending on file path layout, might need expand anonymous function illustration i've given switch()
statement or that.
php spl-autoload-register
No comments:
Post a Comment