Object-oriented development in php

Source: Internet
Author: User
Object-oriented development in php is about to revise a previous project recently. previously written classes basically accumulate some methods, and each class has a size of hundreds of KB, it cannot be an object-oriented object, or a pseudo object. Take a previously written User class as an example to discuss some of the questions. 1. I need to use the $ db variable in the User to query the database. $ db is instantiated through the db class, so I will discuss object-oriented development in php.
Recently, we need to revise a previous project. the classes previously written are basically stacked on some methods. Each class has a size of hundreds of KB, which is not object-oriented at all, or a pseudo object. Take a previously written User class as an example to discuss some of the questions.
1. I need to use the $ db variable in the User to query the database. $ db is instantiated through the db class, then, will my $ db be instantiated in the _ construct () of the User class? The advantage of doing so is that other methods of the User class can directly use the $ db variable. The bad thing is that other methods in my User class may not need to use the $ db variable at all, this is not only the $ db variable, but also some cache objects and webservice objects. that is to say, the object will be instantiated when the method is used, or will it be instantiated in _ construct? Or is there any design pattern to deal with this situation?
II. classification of classes. In the User class, I may add items to users, delete items, and display User item lists, not only items, but also points and money, most of them add, delete, and Display Lists. they all appear to have no problems in the User class and are related to users. However, I personally feel that the User class is purely accumulated by some methods (especially those that display the list). can this be changed? for example, taking points as an example, there is a UserScoreMgr class, it is better to inherit from the User class and put all the points operations in this class? Or what kind of design patterns are there to deal with this problem? Share:
------ Solution --------------------
1. I have been using the Singleton mode to call the static method getInstance () to retrieve objects, and I am used to instantiating classes required by all programs in _ construct. As for where to instantiate it, I think it doesn't matter (it may affect memory usage a little). it is easier to manage it in _ construct.
2. I may divide it like this:
Add or delete an item to display the user item list
Add credits, reduce credits, and display Credits
Increase money, reduce money, and display balance
These can be written as independent classes because they do not affect each other. You do not need to inherit the User. if you think it will be messy, specify these classes to implement a unified interface.
The User class is only responsible for calling, that is, passing some parameters to these classes.
------ Solution --------------------
There is no strict limit. If the business logic is large, layers are required. There are multiple layers of business logic between data and data logic.
The entity stores user information strictly, and then creates user-related business logic.
Users' associated points, items, and money are also linked to the user's business logic.
Of course, there is no perfect design method, and the best fit is the best.
Php object-oriented, "I think few people dare to use it", you are bold. Oh, of course, everything is mixed.
------ Solution --------------------
I don't know the Singleton mode or the factory mode. I only know that it is the underlying implementation mode of php. if you want to use the underlying development mode, all functions such as array and string functions will be unavailable, use the original php iterator directly.
------ Solution --------------------
In fact, the php pattern structure is similar to jsp. If you have a good understanding of the jsp framework. The truth is the same.
------ Solution --------------------
Can php pass classes as parameters?
 
------ Solution --------------------
Your question is refactoring.
Refactoring is to adjust the program code to improve the quality and performance of the software without changing the existing functions of the software, so that the program design mode and architecture become more reasonable, improve software scalability and maintainability.

Split your first classes to form a single object, such as your UserScoreMgr class
It should be noted that the hierarchy of inheritance should not be too many. generally, it is better to have no more than two layers.
------ Solution --------------------
In fact, even if there is nothing too late to look at, it takes only one or two nights to comprehend the spirit.
There are many specific solutions in the book. you can skip to the chapters you need to take a closer look.

The specific refactoring practices are related to your existing code, your requirements, your software architecture, and your personal preferences,
Therefore, I don't think that others can provide more specific solutions when they only view the content written in your post.
You need to do it yourself.



Reference:
Reference:
I don't know the Singleton mode or the factory mode. I only know that it is the underlying implementation mode of php. if you want to use the underlying development mode, all functions such as array and string functions will be unavailable, use the original php iterator directly.

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.