A single file without inheritance implements a common Model, improving code reuse and maintainability

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to compile the Model function. The common method is to create a corresponding Model in the module \ Model directory. class. PHP file to implement the Model function.
If general functions are available, you can create a corresponding Model in the Common \ Model to implement a general Model through inheritance.
In this method, the file structure is clear, but each corresponding Model requires file creation to inherit and implement, and the M method is not powerful.
I do not like this structure. Obviously, by modifying the core load code, you can implement a single-file general Model without creating so many Model files that only inherit the code.
First, we declare that if the modified Code causes exceptions or causes other problems, we recommend that you use the original version by private message or continue to use the original version. In this case, we will not be liable for any losses incurred.

Find the function D function of ThinkPHP \ Common \ functions. php.

Find if(empty($name)) return new Think\Model;Change $parent = class_exists('\\Common\\Model') ? '\\Common\\Model' : '\\Think\\Model';
if(empty($name)) return new $parent;
Find $model = class_exists($class)? new $class($name) : new Think\Model($name);Change $model = class_exists($class)? new $class($name) : new $parent($name);Find $model = new Think\Model(basename($name));Change $model = new $parent(basename($name));Find the function M function in the PHP file.

Find $class = 'Think\\Model';Change $class = class_exists('Common\\Model') ? 'Common\\Model' : 'Think\\Model';The implementation principle is to inherit the Common \ Model to implement a single-file general Model.
Of course, both the D and M methods can be inherited from the Common \ Model after the above modifications.

Create the Application \ Common \ Model. php file,
File template. Remember that the encoding format UTF8 has no signature. Namespace Common;
Class Model extends \ Think \ Model {
Public function test () {// test Method
Echo ('common \ Model test ');
}
}
The reason for not using use Think \ Model above is to avoid repeated conflicts in the current Common \ Model class declaration, and to make the inheritance class better write the Model almost the same as the native Model. For the advantages, see the following code.

If you need to use Common \ Model in the Model of the current Module
The file template is Namespace Home \ Model;
Use Common \ Model;
Class XxxModel extends Model {
Public function test () {// test Method
Parent: test (); // call the Common \ Model: test () method
Echo ('home \ Model test ');
}
}
The implementation of the single-file Universal Controller is relatively complicated, and the stability is also being tested.
It is also very convenient for general-purpose operations such as logon, password modification, and so on.

I spoke about the official code release function, clicked preview, and disabled it. I found that the preview was not _ blank, and the result was revoked, as a result, it is rewritten.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.