Discussion on object-oriented development in PHP

Source: Internet
Author: User
Recently to a previous project revision, previously written classes are basically the accumulation of some methods, a class has hundreds of k size, is not even object-oriented, or called pseudo-object bar. Take a user class that was previously written as an example, and discuss some of the questions in some places.
I in the user to use $DB this variable, used to query the database, $DB is through the DB Class instantiation, then I this $db is not in the user class __construct () in the instantiation of it? The advantage of this is that the other methods of the user class can be used directly with the $DB variable, the downside is that my user class other methods may not need to use the $DB variable, not only the $DB variable, there are some cache object, WebService object, this may be the case, In other words, when the method is used to instantiate the object, or __construct () in the instantiation of it? Or is there a design pattern to handle this situation?
Second, the division of the problem of the class. In the user Class I may have to add items to the user, delete items, display user items list, the same is not only items, as well as points, money and so on, most of them are added, deleted, display the list, these are put in the user class seems to be no problem, are related to the user. But the person always feels that the user class is simply a stack of methods (especially those that show the list), is it possible to change this, for example, with integrals, there is a userscoremgr class, inherited from the user class, and then the operation of the integral is placed in this class, this is not better? Or what design patterns are there to handle this problem?


Reply to discussion (solution)

1, DB Class I have been using singleton mode to invoke the static method getinstance () to fetch objects, and I am accustomed to instantiating all the classes required by the program in __construct (). As for where to instantiate, I do not feel that the impact (may have a little impact on memory), in __construct () in the unified processing instead of easy to manage.
2, I may be divided into this:
Add items, delete items, display list of user items
Add points, reduce points, show points
Increase money, reduce money, show balance
These can be written as separate classes, because they do not affect each other. There is no need to inherit the user, and if you feel the chaos, specify these classes to implement a unified interface.
The work of the user class is simply a call to pass some parameters to these classes

This is not strictly the boundary of the connection. If the business logic is more, it will be layered. In data and data logic, the business logic is divided into layers.
User is the entity that strictly means storing information about users, and then creating user-related business logic.
User-related ancillary points, items, money and so on and related to the user are also mainly focused on the business logic.
Of course, there is no perfect design, suitable is the best.
PHP Object-oriented, "I think few people dare to use", you are very bold. Oh, of course, everything is a mixed blessing.

I do not know what the singleton mode, Factory mode, only know that it is the bottom of PHP implementation mode, if you want to use the bottom-level development, then like the array function, string functions and so on do not use, directly with the most primitive php iterator.

Look <重构> , of course. Other books on design patterns and oo design can also be

In fact, PHP's schema structure is similar to JSP. If you have a good understanding of the JSP framework. In fact, the truth is the same.

Can PHP pass a class as a parameter?

I do not know what the singleton mode, Factory mode, only know that it is the bottom of PHP implementation mode, if you want to use the bottom-level development, then like the array function, string functions and so on do not use, directly with the most primitive php iterator.
It is too late to focus on the problem, there is a solution to the question raised.

The problem you're proposing is refactoring.
Refactoring (refactoring) is to improve the quality and performance of software by adjusting program code without changing the existing functions of the software, so that the design pattern and architecture of the program are more reasonable, and the expansibility and maintainability of the software are improved.

Split your first class to form a single object, like your Userscoremgr class
It should be noted that the level of inheritance is not too many, generally not more than two layers of this advisable

In fact, even if there is no time to see what is too late, looking at only one or two nights, the spirit of understanding.
There are a lot of specific solutions in the book, you can jump to the chapters you need to take a closer look.

The concrete approach to refactoring, and your existing code, the details of your needs, your software architecture, including your personal preferences, are all related,
So, do not think that other people just see your post in the context of such a little content, can give a more specific plan.
You have to do it yourself.



Reference 3 Floor xjl756425616 's reply:
I do not know what the singleton mode, Factory mode, only know that it is the bottom of PHP implementation mode, if you want to use the bottom-level development, then like the array function, string functions and so on do not use, directly with the most primitive php iterator.

It is too late to focus on the problem, there is a solution to the question raised.

No matter, as long as the code maintenance is convenient, clear, not necessarily seriously use what OOP

In fact, OOP and design patterns of the book has been watching, but after all has not done this aspect, hope to stand on your giant's shoulders, less detours, and ultimately have to do on their own. Before doing more listen to the heroes ' opinions, after all, is good.
In fact, even if there is no time to see what is too late, looking at only one or two nights, the spirit of understanding.
There are a lot of specific solutions in the book, you can jump to the chapters you need to take a closer look.

The concrete approach to refactoring, and your existing code, the details of your needs, your software architecture, including your personal preferences, are all related,
So, do not think that other people just see your post in the context of such a little content, can give a more specific plan.
You have to do it yourself.

PHP does object-oriented, isn't that funny? Play with Java drifting over.

  • 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.