PHP5: OOP-code separated from the business logic layer, required?

Source: Internet
Author: User
PHP5: OOP-is it necessary to separate the business logic layer code? I personally feel that the MVC three-layer model is only a subset of the JAVA three-layer development model in the view layer. during daily website development, I am using the MVC-based PHP framework. I used JAVA workflow for project development before. as PH PHP5: OOP -- is it necessary to separate the business logic layer code?
I personally feel that the MVC three-layer model is only a subset of the JAVA three-layer development model in the view layer. during daily website development, I am using the MVC-based PHP framework. I used JAVA workflow for project development. Can I use the previous project development mode as a PHP programmer?

This weekend, we are following this path to test the feasibility from a demo of employee information manager. it turns out that PHP is quite needed, but it can be completely unavailable. this can be a good way to reuse the code, but this code is quite tedious in the process of writing. In addition, every time the PHP page is re-called, it will be re-loaded, created, and initialized. low performance ..
Enterprise applications seem okay, but in this case, PHP's original advantages and convenient lightweight development will be lost...

It may be because I know too little about it to come to this conclusion! The following code: the code is divided according to the java web project directory structure and implemented according to the JAVA three-tier model. The development tool Eclipse3.3

│. Project │ index. php │ webconf. php │ ── WEB-INF │ ── lib │ ── cn │ ── iamsese │ ── core │ ── config │ dbconf. php │ └ ── classes │ └ ── vb2005xu │ └ ── sinlet │ ├ ── service -- here is the logic layer code │ IPersonService. php -- service interface │ ├ ── impl │ PersonServiceImpl. php -- service implementation │ bo ── bo │ Person. php -- business object │ PersonManager. php -- │ PersonRecord. php -- this is implemented by the DAO layer │ ├ ── ui │ ├ ── exception │ dao │ PersonRecordDaoImpl. php │ ── META-INF │ MANIFEST. MF │ ├ ── admin ─ ──. settings │ org. eclipse. php. core. projectOptions. prefs │ example. cache



Because PHP does not have a JAVA package mechanism [it seems that the namespace function has been introduced in PHP6, but I am only familiar with PHP4 and I am just getting started with PHP5]. therefore, we need to define the file loading mechanism of the project and some common configuration parameters here.


Webconf. php -- this is. check the code:
 


Each program in each project has an entry file, and the WEB application is the same. the index. php file here is also. you can think of it as the main method in the JAVA class file. the code is as follows:
 SetManager ($ personManager); $ personRecord = new PersonRecordDaoImpl (); $ personManager-> setRecorder ($ personRecord); // The Code PHP5 is used because all references are passed, therefore, you do not need to use & to pass the reference value return $ personService;} $ personService = getPersonService (); echo"
"; $ PersonService-> addPerson (" "); echo"
"; $ PersonService-> delPerson (" "); echo"
"; $ PersonService-> viewPerson (" "); echo"
"; $ PersonService-> listPerson (" "); echo"
"; $ PersonService-> updatePerson (" ");?>


In index. php file, testing block code. in fact, I want to implement a small IOC here. it is really easy to use PHP to implement IOC. here, I just took it away because it is just a test idea. the code can be found in my previous article.

First introduce the abstract interface file LoadUserClassFile ("vb2005xu/sinlet/service/IPersonService. php ");
This function is defined in webconf. php to load files under the WEB-INF/classes/. directory structure as above.

Now the basic preparations have been completed. let's go to the specific... (* ^__^ ......

 


This line of code is the business object code.
LoadUserClassFile ("vb2005xu/sinlet/service/bo/Person. php ");
 $ Property;}/*** property access controller: set method based on the specified attribute name and value operation class attribute ** @ param String $ property * @ param value $ value */public function set ($ property, $ value) {$ this-> $ property = $ value ;}}?>


PersonServiceImpl. php is a service implementation class:
 Manager;}/*** sets the member information manager object ** @ param PersonManager $ manager */public function setManager ($ manager) {$ this-> manager = $ manager ;} /*** add a member ** @ param Person $ person */public function addPerson ($ person) {$ this-> getManager ()-> addPerson ($ person );} /*** UPDATE group member information ** @ param Person $ person */public function updatePerson ($ person) {$ this-> getManager ()-> updatePerson ($ person );} /*** delete member -- array based on user ID ** @ pa Ram array (int) $ userIDs */public function delPerson ($ userIDs) {$ this-> getManager ()-> delPerson ($ userIDs );} /*** view member information -- View member information based on the specified user ID ** @ param int $ userID */public function viewPerson ($ userID) {$ this-> getManager () -> viewPerson ($ userID);}/*** obtains the member information set based on the query condition List object ** @ param ListObject $ listobject */public function listPerson ($ listobject) {$ this-> getManager ()-> listPerson ($ listobject) ;}}?>



LoadUserClassFile ("vb2005xu/sinlet/service/IPersonService. php ");
See the above interface.
LoadUserClassFile ("vb2005xu/sinlet/service/bo/PersonManager. php ");
This is abstracted layer by layer. it is dispensable, but it is only for expansion. this is what I did in my previous JAVA company. I also raised questions, that's how they answer me .... maybe this is also a small factor for me to leave JAVA...


 Recorder;}/*** sets the member information management data recorder object ** @ param PersonRecord $ recorder */public function setRecorder ($ recorder) {$ this-> recorder = $ recorder;}/*** add a member ** @ param Person $ person */public function addPerson ($ person) {$ this-> getRecorder ()-> addPerson ($ person);}/*** UPDATE member information ** @ param Person $ person */public function updatePerson ($ person) {$ this-> getRecorder ()-> updatePerson ($ person);}/*** delete a member -- based on user I D array ** @ param array (int) $ userIDs */public function delPerson ($ userIDs) {$ this-> getRecorder ()-> delPerson ($ userIDs );} /*** view member information -- View member information based on the specified user ID ** @ param int $ userID */public function viewPerson ($ userID) {$ this-> getRecorder () -> viewPerson ($ userID);}/*** obtains the member information set based on the query condition List object ** @ param ListObject $ listobject */public function listPerson ($ listobject) {$ this-> getRecorder ()-> listPerson ($ listobject) ;}}?>



LoadUserClassFile ("vb2005xu/sinlet/service/bo/PersonRecord. php ");
The record manager that loads employee objects. it is an interface provided to the DAO layer for implementation.

 



At this point, the code at the business logic layer has been basically completed, as long as we implement the PersonRecord interface, we can implement the application.

Look up the getPersonService () function in index. php
LoadUserClassFile ("/vb2005xu/sinlet/dao/PersonRecordDaoImpl. php ");
Looking at its code, there is no specific DAO layer implementation here, only some test code

 





All the codes listed above can run properly in PHP5.1 and later versions.

Summary: it facilitates code separation and accelerates team development. of course, it refers to a team of more than 7 people. but for PHP projects, it is generally impractical and cumbersome.

Thank you for your patience ....

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.