Week7Lab
OOPHP + MVC
OOPHP
When you adopt
object oriented
programming in
PHP, you need to
create PHP classes.
For example:
Book class
BookController class
<?php
Class Book {
private $conn;
public function __construct()
{
$conn = null;
}
// methods
?>
MVC
In MVC, Controllers manage the
Models and Views
For example in Read All Comments
Use Case, BookController manage
Book Model([Link])
List View ([Link])
MVC
Practically, to create MVC in PHP, all
Model, View and Controller need to
be placed in one PHP file.
In the Guestbook application, they
are all placed in
[Link]
[Link]
On top of the file, we can include
(require_once) the model file
([Link])
The middle part of the file consist of
methods or logics to support
interaction between model and view
E.g. view() function is to support
interaction between view_all() method
in [Link] and [Link]
[Link]
In the methods, you should decide
which View file that should be
included or displayed in the browser
like [Link], [Link] and so on.
You should remember that in MVC, all
files are separated according to their
role (model or view or controller)
Role of [Link]
The [Link] role is to divert user
requests to run methods in Controller
like:
[Link]?op=read view()
[Link]?op=delete remove()
[Link] becomes the single point
to refer
How many controllers does a single
web app need?
It really depends on the web app.
In Guestbook example, one is
probably sufficient.
If you were to implement a full blown
app like ecommerce app with
shipping, tax, inventory
management, tiered pricing, etc.,
then you just might want to have a
couple more.