What is the design of PHP object-oriented design and data accessibility?

Source: Internet
Author: User
Ask PHP design for object-oriented design and data accessibility
For example, there is a class person
There is a database operation class Conn
One of the actions in person is to buy a chicken, the chicken information to be put into the database
How should I design it?
is to Perison in the Conn class, then instantiate the Conn object, access the database to insert data, or how to do it?
Please expert advice! PHP object-oriented, database


------Solution--------------------
Reference:
Reference: Class Person extends conn{
function Byji () {
$conn =new conn;//Instantiate a database class
$conn->add ();//Insert Database
}
}

It's better to use the combination mode for this situation. Inheritance is not suitable for use here.


+1
LZ should learn design patterns
Buy Chicken (This event is a bit crazy) from the business logic is not an inheritance relationship, maybe your project is like this or maybe

Business logic determine who (person), action (Buy), what (commodity/chicken) three relationships
Who (subject) operation (variable) what (variable)--visitor pattern, person as abstract class, buy not person's inevitable action
Who (subject) operation (invariant) what (variable)-prototype mode, Buy is person's inevitable action, equivalent to an attribute
WHO (variable) operation (subject and unchanged) what (variable)-bridging mode, buy operation as abstract class, import person buy, and buy commodity chicken
......
You can also use construction mode, shopping cart, order, pay, etc. are sequential relationships, as a method (each introducing conn), person as attribute
Similar to having a session as the main abstract class (combinatorial mode), login, purchase, modify data and other operations as a method (non-ordered relationship), the person class as an attribute of the abstract class
There are other ways of doing it yourself.

But in general, the Conn connection database does not seem to be directly subordinate to the person, but from the one step that belongs to the event
So conn in bridging mode with person, commodity two classes in the abstract class, in the prototype mode and so on level two from the abstract class
Building an event class to instantiate the Conn class as a method so that the combination is good
The more you can subdivide a job, the more combinations you have, of course, in the class, because the function of the class itself is the merging work, and the subdivision of the class is meaningless.

The above is just my thinking angle, design mode is the idea, a pattern can also have a pattern, and everyone's ideas are different, patterns are different, not solid dead
------Solution--------------------
Design patterns are abstracted from a large number of actual project developments.
The purpose of learning design patterns is to let you jump out of your mind and see how people do it. Just that.

In their own development practice, just want to put on a mode, B mode, not yourself to trouble?

------Solution--------------------
I'm going to have to go through the book to get my name, I forgot it.
I believe that everyone has the heart to learn design patterns, and eventually always understand their own combination of the truth

Alas, people are always like this, who is not from the beginning of the primary school teacher let back what on the back, and so slowly learn more, naturally know the original to own "combination" moves
Do not know whether the foreign primary education is so?
  • Related Article

    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.