A note for the PHP instantiation class _ php instance

Source: Internet
Author: User
Recently I have been writing GracePHP5Framework, and I have learned a lot about class instantiation. GracePHP5Framework is a completely MVC-based framework with excellent scalability. It can be very flexible for class calls. The following describes how to call a model function. The basic function of this function is to specify the name of a model (abstracted as a class), and then it will find the script of this class under the model directory and return it after instantiation. One of the advantages of this approach is that loading and instantiation are automatic, and you can get the maximum flexibility. The following code is not long and complex:

Function & load_class ($ class_name, $ param = null, $ instantiate = true)
{
Static $ objects = array ();

$ Class_name = ucfirst (strtolower ($ class_name ));
If (isset ($ objects [$ class_name]) {
Return $ objects [$ class_name];
}

$ Class_file = DIR_MODELS. "/{$ class_name}. inc. php ";
If (file_exists ($ class_file )){
Require_once $ class_file;

If (! Class_exists ($ class_name )){
Return false;
} Else {
$ Objects [$ class_name] = & new $ class_name ($ param );
Return $ objects [$ class_name];
}

} Else {
If ($ instantiate ){
$ Objects [$ class_name] = null;
}
Return null;
}
} The function has only three parameters: $ class_name, $ param, and $ instaniate. $ param is the constructor parameter and $ instaniate is optional. Note that the $ objects array in the function is a static variable, that is, when this function is called, the array will not be released, and the data in this array will be saved the next time you call this function. The advantage of doing so is that you can directly return the instances of this class if you need to call them again after most of the class instances are completed. This avoids repeated calls and improves performance. The Code is as follows:

Static $ objects = array ();

If (isset ($ objects [$ class_name]) {
Return $ objects [$ class_name];
} The other code that continues is to check whether there is a file with the class name. If there is a file loaded and the class with the specified name is searched, it will be instantiated after the class is found. This requires that the class name in the script must be consistent with the script file name. I think this is also conducive to future code management.

The $ instaniate parameter is effective at this time. This parameter tells the function to make a tag bit (null) under $ objects if it is not found) this prevents the function from repeatedly searching for file names and loading and searching them.

$ Class_file = DIR_MODELS. "/{$ class_name}. inc. php ";
If (file_exists ($ class_file )){
Require_once $ class_file;

If (! Class_exists ($ class_name )){
Return false;
} Else {
$ Objects [$ class_name] = & new $ class_name ($ param );
Return $ objects [$ class_name];
}

} Else {
If ($ instantiate ){
$ Objects [$ class_name] = null;
}
Return null;
} Statements:

$ Objects [$ class_name] = & new $ class_name ($ param); you can repeat it multiple times. $ Class_name is a string variable in the function. The new Keyword can be used to dynamically instantiate the class of a specified string (if any ). For details about this call method, refer to the PHP manual and here.

The disadvantage of this function is how to pass different numbers of parameters to constructors of different classes. It may be possible to use functions such as call_user_func_array, but this approach is not Grace. You need to repeat it here. In fact, tests on file_exists and other files can be handed over to the _ autoload function for processing. However, because other functions such as interface_exists also call the _ autolaod function for compatibility consideration, therefore, we only perform a simple test in the function.

PHP5 is more object-oriented than PHP4. I think it's time to update our coding ideas. For PHP 5 classes and objects, here is a very good tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.