using require inside a class
i want to make a "loader class" that will require selected files.
so i just can c开发者_如何学Pythonall eg. loader::load('systemLibraries, 'applicationLibraries').
so inside this load() method i will use require. but i have tried this and it seems that the files required can't be used outside the class.
how can i make it globally accessed?
This should work fine:
class Loader{
function load($class_name)
{
require($class_name ".php");
}
}
Loader::load("MyClass");
$class = new MyClass;
Given that MyClass is in "MyClass.php"
This on the other hand, won't work
class Loader{
function load($class_name)
{
require($class_name ".php");
$class = new $class_name;
}
}
Loader::load("MyClass");
$class->doSomething();
If include.php
looks like this
$var = "Hi";
You can't do this:
Loader::load("include");
echo $var;
As there are scope issues.
You are going to need to give us more information on exactly what you are trying to access.
Yes, as Chacha pointed out, make sure that you create the instance of the classes outside of your loader class. And since you have used the term system libraries which are usually always needed by the system, you can use the __autoload magic function to included them all automatically for you.
精彩评论