简体   繁体   中英

PHP with OOP and MVC

Using PHP 5.x

Question, lets say I want to display results from a database. Lets say the last 30 stories from the database. Using OOP and MVC would the ideal setup be you have a class that connects to the db, a class that queries the database to get the information then a class that handles the display of the results and then a page that puts it all together?

So basically I would pass the db connection object to the class that gets me the story results from the database Then pass the story class object to the display class to build the view then pass that back to the view itself and echo out the value? Hopefully this makes sense, just trying to see if Im understanding this:

dbconnect_class.php

storyresults_class.php

storydisplay_class.php

Include all 3 into a page such as display_stories.php, which I believe is the controller in mvc, then run the code and display it in the view which would be in its basic form an include to a template file.

MVC meaning Model -> View -> Controller. In most of the frameworks the request comes to the Controller, the Controller asks the Model to retrieve the database results then the same Controller sends the results to the view in order to display them so that's the basic breakdown of a MVC application.

Don't pass the results object around. That will mean the display class needs to know the implementation within that class.

What you can do is let the controller get the results then pass the results to the display class. You can make a data class that keeps the data in a suitable object which you can then pass to the display class.

However, this is just some general oop advice but for better mvc, you need to read on that. Some links have been provided by others.

Unless your using a template engine it would be very difficult to not write php code in a view file. At the company I work for, we have developed a set of UI components that allow us to bind data to a component in the controller and have that component render on the view, similary to how asp.net controls work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM