The first project to solve the bug of repeatedly calling the model class in brophp with a namespace is completed. although I am not satisfied with this project, it is still over in any case. Because I have a small foundation and do not want to use process-oriented and oop-oriented projects, I plan to use a framework for the first project. so I will study the brophp code before the project, on the basis of brophp, I made a bug of "using namespaces to solve repeated model class calls in brophp ".
The first project is completed. although I am not satisfied with the project, it is still finished in any case.
Because I have a small foundation and do not want to use process-oriented and oop as a project, I plan to use the framework for the first project.
So I studied the brophp code before the project and made a "self" framework based on brophp.
I said it was my own. In fact, I only understood brophp and rewritten it against this brophp.
This bug was discovered by accident in the project.
At that time, I was at the front-end of common. class. php calls D ('user', 'admin') to verify user permissions, and then uses D ('user') to update user data in another controller.
In this case, the error message is displayed, indicating that the UserModel class cannot be repeatedly defined. this is a problem caused by calling two classes with the same name.
Just as we cannot use memcache or mysqli for class naming
At that time, the project was busy, and I did not dare to think about it. I just considered the easy solution: rename both classes and add the project name.
After the project was completed, I fell asleep for two days. I thought about this problem again this evening and thought about it carefully. I suddenly remembered that the namespace added by php5.3 was added to solve this problem.
I reviewed the manual and studied this new thing. it actually solved this problem.
Add the namespace definition in the header when generating the model compilation file
For example, home's UserModel. php
-
- Namespace home;
- Class UserModel extends \ Dpdo {
- }
Usermodel. php of admin
-
- Namespace admin;
- Class UserModel extends \ Dpdo {
- }
Note that the namespace defined before Dpdo is not defined in other files, so it belongs to the global space. "if no namespace is defined, all classes and functions are defined in the global space, it is the same as before PHP introduced the namespace concept. Prefix before name
\Indicates that the name is in the global space, even if it is in another namespace. "
Modify the D method so that the model is instantiated in the following format:
- $ H = new home \ UserModel ();
- $ A = new admin \ UserModel ();
This solves the problem of this duplicate call class.
The specific code will not be sent. if you are interested, try it out.
In fact, the simplest way is to unify the instance. for example, if you use D ('user', 'admin'), this problem will not occur.
But this is to escape the problem, rather than solve the problem.