Specification | Development Guide
Fleaphp's naming rules and directory structure look more complicated at first, but after you get used to it, you'll see a lot of benefits from this naming convention. The same approach is therefore used in the Zend Framework.
Of course, fleaphp has no mandatory requirements for application naming rules and directory structures. Simply using consistent naming conventions and directory structures makes it easy to maintain applications. It is also easier to collaborate with fleaphp application developers.
Naming rules for global functions
The naming convention for global functions is to separate all lowercase words with "_". such as Get_cache (), Echo_h (). At the same time, the name of the function takes the form of "verb + object". For example, write (verb) _cache (object).
Some global functions, however, are consistent with similar functions in PHP, using different naming methods (by the way, PHP's own global function naming is confusing, all forms are available).
Naming rules for classes
All fleaphp's own classes begin with Flea_. Then name it according to the purpose. such as Flea_controller_action, Flea_helper_imgcode. then Replace "_" in the class name with the directory separator, which is the location of the definition file for this class.
Example:
Flea_controller_action class, save file as flea/controller/action.php
Flea_db_tabledatagateway class, save file as flea/db/tabledatagateway.php
The advantages and disadvantages of this naming convention and directory structure are as follows:
Advantages:
- Avoid naming conflicts
- File storage location can be found by class name
- You can use the import (), Load_class () functions of fleaphp in your application to easily load class definition files
- If you use PHP5, it is convenient to use __autoload () to automatically load the required class definition:
<?phpfunction __autoload($className) { load_class($className);}?>
Disadvantages:
- Class names are longer and are not easy to enter, such as Flea_com_rbac_usersmanager
- Maybe it's different from the existing habits and it takes time to adapt.
For the first drawback, using an IDE such as Zend Development environment or Eclipse can be mitigated. These ides provide powerful input autocomplete, typically by entering the first few letters of a class name, and a candidate list is displayed for the developer to choose from.
Variable and constant naming
In fleaphp, variables are divided into global variables, temporary variables, and template variables.
- Global variables and constants
Global variables and constants are all capitalized, separated by "_". For example $GLOBALS['CLASS_PATH']
and FLEA_DIR
. However, the use of global variables and constants is avoided in fleaphp, so there is generally no conflict with the application.
- Temporary variable
Variables used in functions, class methods are temporary variables, and the naming rule is the first word lowercase, followed by the first letter of the word uppercase. For example $requestFilters
, and $dispatcherClass
so on.
- Template variables
Template variables are listed separately because different template engines have different specifications for variable names. Personally, I prefer template variables to use all lowercase words and "_" to separate them, for example $latest_products_list
. This allows you to see which variables are used in the template in a single glance in the program.
Directory Structure of Fleaphp
A typical fleaphp application has the following directory structure:
The directory of this application is mainly divided into weblibs and WebRoot. Where Weblibs saves all the code for the program, and the WebRoot directory saves only those parts that the user can access, such as index.php entry files, pictures, CSS stylesheets, and JS scripts.
The weblibs is divided into FLEA and YORK two directories. The FLEA directory holds the core files of the fleaphp, while the YORK directory holds the code for the application. It was named York because the company of the project was named York. So developers in planning the directory structure, you can refer to this way, to save the application code directory to take an easy to understand name.
WebRoot is to save content that users can access directly using the browser, so you need to modify the server or virtual host settings to point the root directory of the Web site to the WebRoot directory. For example www.example.com points to/example/webroot/.
The directory structure described here has the following advantages:
Advantages:
- Code files are placed where the browser cannot access, improving security
- Programmers and designers can work in different directories, reducing the chance of conflict
- More clear directory structure, easy to understand
The main disadvantage is that you need to modify the server settings and point the root directory of the site to the WebRoot directory. Some virtual hosts do not allow this, or need to contact the server administrator, more cumbersome.
For this directory structure, the index.php entry file placed in WebRoot should explicitly invoke the import()
Weblibs directory to be added to the class definition search path. Otherwise load_class()
, you will not be able to find the file you want.
<?phprequire('../WebLibs/FLEA/FLEA.php');import('../WebLibs/YORK/');....run();?>
For virtual host users, you can move the Weblibs directory to the WebRoot directory. form the following directory structure:
In the illustration above, all files are saved in the application's root directory CDHLSS, and the code files are saved in the Libs subdirectory. Where Libs/app saves the application's code.
The corresponding index.php need to be modified to:
<?phprequire('libs/FLEA/FLEA.php');import('libs/APP/');....run();?>
In actual development, because fleaphp does not enforce a naming convention and directory structure. So developers can continue to develop applications in ways that are familiar to them.