PHP object-oriented design and data access design

Source: Internet
Author: User
Ask for 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!


Reply to discussion (solution)

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

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

I'm glad you can reply to me. that's what I think, but my colleague said it's best not to write database-related code in the class.
I don't know if it's an MVC model or something?

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.

Reference the nowphp reply on the first floor: 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. I am taught how to write it.

You can encapsulate the database operation class conn into a database operation class, and directly call the static conn method add in person.

Reference the nowphp reply on the first floor: 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) operations (variable) and what (variable )?? Visitor mode. as an abstract class, buying person is not an inevitable operation of person.
Who (subject) operations (unchanged) and what (variable )?? Prototype mode. buying is an inevitable operation of person, which is equivalent to an attribute.
Who (variable) operations (subject and unchanged) and what (variable )?? Bridge Mode. the buy operation is used as an abstract class. import the product chicken bought by person and bought by buyer.
......
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.

How can this problem be solved?
Do not fall into the trap of others

How can this problem be solved?
Do not fall into the trap of others

Haha, good advice

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?

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?

Thanks for the guidance of xuzuning and snmr_com. I am too limited to the MVC. it is best to have my own design pattern without sticking to a certain pattern.

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.