How to organize your own classes in FuelPHP?
题
I put all my classes in "fuel/app/classes/" folder, for a simple call to MyClass::MyMethod()
Start to store classes in "fuel/app/classes/lib/" with the given "namespace lib".
Calling the lib\MyClass::MyMethod()
or prescribing "use lib" much more comfortable than proposed Folder_MyClass::MyMethod()
see http://docs.fuelphp.com/general/classes.html
Is there any way to load a specific namespace is in "bootstrap.php", that would not have to write any "use lib" at the beginning of the file (controller, model), or not add everytime to call like "lib\"?
解决方案
You could probably add lib
as a Core Namespace. In bootstrap.php
:
Autoloader::add_core_namespace('lib');
http://docs.fuelphp.com/classes/autoloader.html#/method_add_core_namespace
You should then just be able to use \MyClass::MyMethod();
without having to prefix it with the namespace or without having to Use lib;
其他提示
FuelPHP uses a cascading filesystem for classes that doesn't favor class name segments over namespaces. The only requirement is that you classes are somewhere in the classes folder.
For example, say you have a file called app/classes/some/sub/system/name/myclass.php.
You can define this class as:
class Some_Sub_System_Name_Myclass {}
But also as
namespace Some\Sub\System\Name;
class Myclass {}
Or anything in between. The common rule is : glue your namespace and class name together, convert it to lower case, replace all underscores and backslashes by DIRECTORY_SEPARATOR, and stick a ".php" extension on it, and you have your filename.