Could you tell me the PHP object-oriented design and data access design?

Source: Internet
Author: User
Ask for PHP object-oriented design and data access design. for example, if one class Person has a database operation class ConnPerson, one operation is to buy a chicken, how should we design chicken information in the database? Whether to include the Conn class in perison, instantiate the Conn object, access the database to insert data, or how to get it? Please advise! Php: PHP object-oriented design and data access design
For example, there is a class Person
There is a database operation class Conn
One operation in Person is to buy a chicken and put the chicken information in the database.
How should we design it?
Whether to include the Conn class in perison, instantiate the Conn object, access the database to insert data, or how to get it?
Please advise!

Php object-oriented, database


------ Solution --------------------
Reference:
Reference: class Person extends Conn {
Function byji (){
$ Conn = new Conn; // instantiate the database class
$ Conn-> add (); // Insert a database
}
}

It is better to use the combination mode in this case. Inheritance is not suitable here.


+ 1
LZ should learn the design mode
Buy a chicken (this event is a little crazy) from the business logic, it is not an inheritance relationship, maybe your project is like this.

The business logic determines who (person), Operation (Buy), and product/chicken relationships.
Who (subject) operation (variable) what (variable) -- Visitor mode, person as an abstract class, buy is not an inevitable operation of person
Who (subject) operation (unchanged) what (variable)-prototype mode, buy is an inevitable operation of person, equivalent to an attribute
Who (variable) operation (subject and unchanged) what (variable) -- Bridge Mode, buy operation as abstract class, import person buy, and buy commodity chicken
......
You can also use the construction mode. shopping cart, order, and payment are sequential relationships. as a method (conn is introduced for each method), person is used as an attribute.
Similar to using a session as the main abstract class (combination mode), logging on, purchasing, modifying data, and other operations are used as methods (non-ordered relations), and the person class is used as an attribute of the abstract class.
There are other combinations. study them by yourself.

However, in general, conn cannot directly connect to the database from the person, but from the event step.
Therefore, in the bridge mode, conn is tied to the abstract class in the person and product classes. in the prototype mode, the second-level conn belongs to the abstract class.
Create an event class and instantiate the conn class as a method. this combination is better.
The more work segments you can divide, the more combinations you can combine. of course, the unit is class, because the function of the class itself is to merge the work, and the subdivision under the class is meaningless.

The above is just my perspective of thinking. the design model is the idea. there can also be patterns in a certain model. different people have different ideas and different models are not fixed.
------ Solution --------------------
The design pattern is abstracted from a large number of actual project development.
The purpose of learning the design model is to let you jump out of your own limitations and see how people do it. That's all.

In your own development practices, you have to stick to the and B modes. Aren't you looking for trouble for yourself?

------ Solution --------------------
I have forgotten the name even if I have to flip a book.
I believe that everyone is willing to learn the design model and will eventually understand the principle of their combination.

Alas, people are always like this. no one starts from elementary school, and when the teacher starts to learn more, the teacher knows that he wants to "combine" his/her skills.
I wonder if this is true for foreign primary education?

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.