I was studying a couple PHP frameworks and then decided to build my own, of course. But i'm facing one issue. I have a Router class that handles dynamically the HTTP requests and it basically explodes the URL into elements dividing it by the slash and storing it into an array, then a function is called to check if the first element is a valid Controller. If it is valid, the function should require it, but that's where i'm stuck, because it seems that i can't require a file like:
if (file_exists(CONTROLLERS_DIR . $this->url[0] . '.php')) { require \App\Controllers\$this->url[0] }
How can I require a file like that using namespaces?
Thanks.
"How can I require a file like that using namespaces?"
You can't. Namespaces have nothing to do with it.
"PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants." ~ Namespaces overview
require is about file dependancies, regardless the namespace:
if (file_exists(CONTROLLERS_DIR . $this->url[0] . '.php')) {
require(CONTROLLERS_DIR . $this->url[0] . '.php');
}
EDIT: You might want to instantiate a class using a namespace and class name retrieved in run-time though, i.e. something like:
namespace \App\Controllers;
class C {
protected $_i;
public function __construct($i){ $this->_i = $i; }
public function foo(){ echo $this->_i; }
}
and somewhere:
$className = "C"; // or $className = $this->whatever...
$class = "\\App\\Controllers\\".$className;
$instance = new $class(7);
$instance->foo(); // outputs 7
I've built couple of frameworks and I understand what you trying to do...
Basically when you have some path for example "HelloWorld\addComment"
You want to create controller instance
\App\Controllers\HelloWorldController
There are multiple ways to solve it, the one I like is:
Using spl autoloader
http://php.net/manual/en/function.spl-autoload.php
In the link I provided you got the examples you need.
Then you can end up just doing
$controller = new \App\Controllers\HelloWorldController();
You should put the HelloWorldController at the right namespace + maintain directory structure matching the namespace
app
Controllers
HelloWorldController
spl autoloader will do the right including for you, often the default implementation is sufficient - but it is easy to create your own spl autoloader and register it
Later you can test if the $controller has the method you need via method_exist or reflection...
Related
I have this function below that autoloads classes before registering them as new classes.
function __autoload($controller){
$ce = explode('\\', $controller);
require ROOT . '/app/base/classes/' .
end($ce) . '/class.' .
end($ce) . '.php';
}
How can I ignore this autoloader for one class? The reason behind this is because I installed a package and the class file is another directory to what my class files are in...
Try to require your package file containing the class you need, before calling new OneClass().
For example, if you have [ROOT]/app/base/classes/OneClass/class.OneClass.php alongside with [ROOT]/app/custom/packages/OneClass.php, you may:
require ROOT . '/app/custom/packages/OneClass.php';
$class = new OneClass(); // will be an instance of /app/custom/packages/OneClass.php
But the best solution is using namespaces, as described in PSR-0 and PSR-4 recommendations.
And when you use namespaces, and you have correct directory/file structure/names, and you have correctly described namespaces inside your classes:
$my_class = new \app\base\OneClass();
// The same class name without any conflicts
$class_from_package = new \app\custom\packages\OneClass();
I think it's a more useful approach. You will have some logical directories/files structure which will reflect namespace structure. And if you want load the same class name from other folder, you just will be able to do it by using fully qualified class name with correspond namespace.
It can be a bit weird, to use namespaces, if you did not use them before. You need to support correlations between file path and namespace definition all the time (when moving class file to another folder, for example), but after some time of using it, you will pay no more attention to namespaces than you are paying to tying your shoes before going outdoor.
You can do something like this:
function __autoload($controller){
$ce = explode('\\', $controller);
$ignore=array('class_1', 'class_2');
if (!in_array(end($ce), $ignore)) {
require ROOT . '/app/base/classes/' .
end($ce) . '/class.' .
end($ce) . '.php';
}
}
I want to write a class autoloader to use in a wordpress plugin. This plugin will be installed on multiple sites, and i want to minimize the chance of conflicts with other plugins.
The autoloader will be something like this:
function __autoload($name) {
//some code here
}
My main issue is, what happens if another class also uses a function like this? I think it will be bound to give problems. What would be the best way to avoid something like that?
I am trying to not use namespaces so the code will also work on previous versions of php.
Use some implementation like this one.
function TR_Autoloader($className)
{
$assetList = array(
get_stylesheet_directory() . '/vendor/log4php/Logger.php',
// added to fix woocommerce wp_email class not found issue
WP_PLUGIN_DIR . '/woocommerce/includes/libraries/class-emogrifier.php'
// add more paths if needed.
);
// normalized classes first.
$path = get_stylesheet_directory() . '/classes/class-';
$fullPath = $path . $className . '.php';
if (file_exists($fullPath)) {
include_once $fullPath;
}
if (class_exists($className)) {
return;
} else { // read the rest of the asset locations.
foreach ($assetList as $currentAsset) {
if (is_dir($currentAsset)) {
foreach (new DirectoryIterator($currentAsset) as $currentFile)
{
if (!($currentFile->isDot() || ($currentFile->getExtension() <> "php")))
require_once $currentAsset . $currentFile->getFilename();
}
} elseif (is_file($currentAsset)) {
require_once $currentAsset;
}
}
}
}
spl_autoload_register('TR_Autoloader');
Basically this autoloader is registered and has the following features:
You can add a specific class file, if not following a specific pattern for the location of the files containing your classes (assetList) .
You can add entire directories to your search for your classes.
If the class is already defined, you can add more logic to deal with it.
You can use conditional class definition in your code and then override your class definition in the class loader.
Now if you want want to do it in OOP way, just add the autoloader function inside a class. (ie: myAutoloaderClass) and call it from the constructor.
then simply add one line inside your functions.php
new myAutoloaderClass();
and add in the constructor
function __construct{
spl_autoload_register('TR_Autoloader' , array($this,'TR_Autoloader'));
}
Hope this helps.
HR
I recommend using namespaces, and PSR-4. You could simply copy this autoloader example from FIG.
But if you don't want to, you can use an autoloader like this one, that defines an convention for WP classes names, and uses that to find the class file.
So, for example, if you call a 'Main' class, then this autoloader will try to include the class file from path:
<plugin-path>/class-main.php
I have an interface which I named Manager, and two classes UtilisateurManager and StageManager, and the both of them implements the Manager interface.
In each class I included the interface Manager as the following :
require '../helpers/Manager.class.php';
Then I needed to use these two classes at once in a php script, but I got the error that I can't redeclare class Manager.
I tried to work with the function class_exists() but it's not useful in my case.
How can I solve this problem ?
Maybe you can use require_once instead of require.
require_once '../helpers/Manager.class.php';
You need to implement a simple autoloader which will try to load class/interface only if it isn't already loaded, and forget about direct require/include then. It is considered good practice to leave the filesystem details out of the class file: you may reuse it in other project where your directories system might change, so you'll need to edit the class again.
A simple example:
class Autoloader
{
protected $directories = array();
public function register()
{
// register 'loadClass' method as an autoloader
spl_autoload_register(array($this, 'loadClass'));
}
public function addSource($dir)
{
$this->directories[] = $dir;
}
public function loadClass($className)
{
foreach ($this->directories as $dir) { // search in every registered sources root
$fileName = $dir.DIRECTORY_SEPARATOR.$className.'.class.php';
if (file_exists($fileName) {
include($fileName);
return; // halt when file was found
}
}
}
}
// bootstrap file
$autoloader = new Autoloader;
$autoloader->register();
(spl_autoload_register documentation)
Please note that this realization will cycle through the folders, and this is certainly not the desired solution. The best option today is to build project according to PSR-4 spec - autoloader implementation will have to check existence of a single file only. It may be a little hard to dive into namespaces without any preparation, but once you get familiar with this coding style, you will forget about direct file load.
The last thing, i want to warn you about your naming convention - your Interface is stored in a .class.php file, and this is confusing.
I've been looking around quite a bit and can't find a concise answer to this question anywhere- perhaps those of you with more experience autoloading than myself could shed some light. Pretty simple question:
I wrote a simple autoloader:
function __autoload($className){
$className = BASEDIR . "include/class/" . str_replace("_", "/", $className) . ".php";
require_once($className);
}
Where BASEDIR is the base project directory. Works great including classes- go figure.
However, when I go to include classes that extend other classes, it starts to break.
Per the suggestions on this site, I'm using PEAR naming conventions for my classes. So my "Event" class (describing an actual event like a party) lives here:
include/class/Event/Event.php
The actual class name is "Event".
When autoloading the class, I reference it like so:
new Event_Event();
The autoloader turns that into:
"include/class/Event/Event.php";
The problem comes when Event is extended by some other class utilizing this naming convention.
I'm using, for example:
class EventInstructed extends Event_Event
The autoloader knows where to look, but since my Event class name is "Event" and not "Event_Event", it is trying to look for the wrong class and can't find it.
The question therefore is, how do I utilize the pear naming conventions correctly, or should I do extra parsing in my autoloader to fix my issue? Explode the string on underscores, grab the class name and use that instead?
Naming conventions are here:
http://pear.php.net/manual/en/standards.naming.php
Thanks!!
edit
My final solution was as mentioned in the accepted answer. Classes are in the hierarchy as such:
include/class/Event/Event.php
include/class/Event/Lesson.php // <-- extends Event
Inside the Event file:
abstract class Event_Event{
//Class code....
}
Inside the Lesson file:
class Event_Lesson extends Event_Event{
//Class code....
}
And finally, the __autoload function:
define("BASEDIR", __DIR__ . "/");
function __autoload($className){
$className = BASEDIR . "include/class/" . str_replace("_", "/", $className) . ".php";
echo $className . "<br / >";
require_once($className);
}
You should always add the namespace like this:
class Event_Instructed extends Event_Event
The file name should be Instructed.php in this case.
You should specify a namespace... Like this:
namespace Event
{
abstract class Event {}
class Lesson extends Event {}
}
this shold work just fine with your autoload function.
P.S.: __autoload function is deprecated. Try making this:
function Autoload ( $Subject )
{ /* ... */ }
spl_autoload_register('Autoload');
This way you'll not need the str_replace in your Autoload function...
You'll also be able to extend in other namespaces..
namespace OtherNamespace
{
class AnotherEvent extends Event\Event {}
}
I understand how to register autoloaders and even how to create one, that's no issue at all. How ever the main issue is - how do you have two auto loaders running side by side for something like:
class project_one_folder_class extends project_two_folder_class{}
You'll notice that the child class belongs to a project which is reaching out and calling the parent class which is locate in a different project.
The way the projects are linked project two's classes are always seen by the auto loader, how ever project one's classes are never seen.
So the way I thought around this was to write two auto loaders and register them because php will look in on then the other. How ever php seems to be looking in only one and not the other.
how would you solve this?
Edit
Project two is parent, Project one is child. This is more expanded question then What was posted on this question.
To better expand this is my class.
class AisisCore_Loader_AutoLoader{
protected static $_instance;
public function get_instance(){
if(null == self::$_instance){
self::$_instance = new self();
}
return self::$_instance;
}
public function reset_instance(){
self::$_instance = null;
}
public function register_auto_loader(){
spl_autoload_register(array($this, 'load_class'));
spl_autoload_register(array($this, 'load_child_class'));
}
public function load_class($class){
$path = str_replace('_', '/', $class);
if(file_exists(get_template_directory() . '/' . $path . '.php')){
require_once(get_template_directory() . '/' . $path . '.php');
}
}
public function load_child_class($class){
$path = str_replace('_', '/', $class);
if(file_exists(get_stylesheet_directory() . '/' . $path . '.php')){
require_once(get_stylesheet_directory() . '/' . $path . '.php');
}
}
}
Currently this class will load anything in the parent project. It will even load parent project objects in the child project. How ever no child object can be loaded using this class as it is not found.
Those familiar with WordPress will instantly say, yes its because you have get_template_directory when you want get_stylesheet_directory How ever - Knowing this - I want to write then two auto loaders, one that will load child projects objects using get_stylesheet_directory and then one that will load parent objects via get_stylesheet_directory so that:
class project_one_folder_class extends project_two_folder_class{}
works and loads, with out error.
This is a little rough, but you actually only need one autoloader that just checks within multiple directories:
abstract class Hakre_Theme_AutoLoader
{
public static $directories;
public static function init()
{
// configure paths
self::$directories = array(
get_template_directory(), // current theme, parent in case of childtheme
get_stylesheet_directory(), // current theme, child in case of childtheme
);
// please double check if you really need to prepend
return spl_autoload_register('Hakre_Theme_AutoLoader::autoload', true, true);
}
public static function autoload($class)
{
$filename = str_replace('_', '/', $class) . '.php';
foreach (self::$directories as $directory) {
$path = $directory . '/' . $filename;
if (is_file($path)) {
require($path);
return; # leave because the class has been loaded
}
}
}
}
Hakre_Theme_AutoLoader::init();
## If you're unsure all worked out properly:
var_dump(Hakre_Theme_AutoLoader::$directories);
This should actually do it, however I've not tested it. See the var_dump, you can debug if the correct directories are registered or not. No need to have multiple callbacks only because you want to check in two directories.
Well, of course you could just look up a second path when the first file_exists returns false - in the very same autoloader.
You can also register a second autoloader for the second path.
PHP docs addresses this:
If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.
Separate the autoloading process!
PHP is working like this: At some point, the code execution stumbles upon a class reference that is not yet loaded. $obj = new project_one_folder_class()
Now the registered autoloader functions are called, starting with the one added first (unless the ones added later are using the "prepend" parameter).
The first (and all subsequent) autoload function gets the name of the class that should be loaded. The function now has the task to decide
Is it it's task to load this class? If not, do nothing, let the other functions try to load it.
If this function is responsible to load the class, try to make up a path and filename out of the known class name, and require this file.
Step 1 is usually solved by looking if the class name to be loaded starts with a certain prefix. In your case, the autoloader responsible for loading classes starting with "project_one_" should NOT try to load any other classes starting with something else.
Step 2 takes place if the autoloader knows that it must perform the autoloading. It now transforms the class name into a relative path and filename, adds the base directory, and then requires the file.
Requiring a file that defines a class which extends another class (or implements interfaces) can then trigger another run of autoloading with any still unknown classes. In your case, the autoload process is started again with the class name "project_two_folder_class". Note that the first autoloader for "project_one_" classes will be called again with this second class name, but it must not do anything, because it does not know how to load these classes. This is up to the autoloader that knows about "project_two_" classes.
Order of registration should not matter for the different autoloaders. If it does, autoloaders misbehave.
Because it is a common pattern to simply transform any underscore characters in class names into DIRECTORY_SEPARATOR, as well as the backslashes from namespaces, add ".php" at the end, and then try to load this file from a defined base directory, the code for two of these autoloaders will be identical, and the problem is reduced to configuring only one autoload function with these two cases: Which prefix should be present, and in which base directory should the file be expected to be, provided that the class name can be transformed into a relative path to the file.
Checking in every case whether the file exists in a certain directory or not is not optimal for performance. You'll end up doing plenty of checks for files that you will be able to know you cannot load by simply looking at the class name.
And by the way: Autoloader functions do not need to be singletons. In fact, your singleton implementation is broken, and useless. Instantiate the autoloader with new, maybe pass some configuration values into the constructor, and call spl_autoload_register(), and you should be done.
You can add more than one loader, just register each function with spl_autoload_register().
procedural style:
spl_autoload_register('loader_function1'));
// your second class location
spl_autoload_register('loader_function2'));
or in a OO style
spl_autoload_register(array($this, 'loader_function1'));
spl_autoload_register(array($this, 'loader_function2'));
However based on your question: you probably can accomplish the same thing by having a more flexible autoloader
see my answer in a different question here:
Class autoloader in wordpress plugin