Evaluate oop display strategies in tears

Source: Internet
Author: User
Tear in the tears to find the oop display policy 1. basic situation oop writes the conn to connect mysql class, all the classes that use the database in the system to extends it. For more information about this section, see class & nbsp; db {public & nbsp; $ conn; & nbsp; private & nbsp find the oop display policy in tears.
1. Basic information
Conn written by oop connects to mysql. all Database classes in the system are extends.
For more information, see:

Class db {
Public $ conn;
Private static $ h = "localhost ";
Private static $ u = "root ";
Private static $ p = "123123 ";
Public $ db = "wms"; // It is also changed. In other words, wms is not fixed and changed. When user1 logs on, $ db = "user1"; when user2 logs on, $ db = "user2 ";

2.
Because $ db is changing, not fixed. I started with $ db =$ _ SESSION ['user'] but the session cannot be used in the class.
How can this problem be achieved?
3.
This method is excluded.
$ Obj = new db ($ db); // use the destructor. This method cannot be used. It cannot be used for other reasons.
------ Solution --------------------
1. inherit the link class in this way
require_once 'db.php';
class userDao extends db{}

2. ① it is best not to design the database like this. it is very complicated to expand and maintain the database later. LZ should consider different situations. it is best to start from multiple tables or fields.
② If the session value is to be passed to the class for processing, it is first passed through the PHP processing page (we generally call it the control layer) and passed through the callback class method.
For example, after user1 is logged on, the processing page is marked as 1. before accessing the database, input 1 to let him know which database to call. However, we recommend that you do not design the database too complex.
3. Why does LZ need to instantiate an object in the destructor? Generally, destructor are used to destroy objects and release space. LZ should initialize the object in the constructor.
_ Construct ()

------ Solution --------------------
Why can't I use constructor ............
------ Solution --------------------
Didn't you say that?


Write an init function to pass the value.
$ Db = new mydb ();
$ Db-> init ($ dbname );
------ Solution --------------------
1 man, big husband, Don't Cry at will ~
2. we recommend that you start from using the framework before developing product projects and learning and researching these problems.
In the framework, there is usually a good encapsulation for db classes, and you don't have to worry too much about it.
------ Solution --------------------
$ Obj = new db ();
$ Obj-> db =$ _ SESSION ['user'];

Since it is a public attribute, just assign a value directly.

------ Solution --------------------
Write an init function to pass the value.
$ Db = new mydb ();
$ Db-> init ($ dbname );
S

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.