Solve the bug of repeatedly calling the model class in brophp using the namespace

Source: Internet
Author: User
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
  1. Namespace home;
  2. Class UserModel extends \ Dpdo {
  3. }
Usermodel. php of admin
  1. Namespace admin;
  2. Class UserModel extends \ Dpdo {
  3. }
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:
  1. $ H = new home \ UserModel ();
  2. $ 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.


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.