A very interesting question that brings us back to the centuries-old discussions on procedural and OO programming. Why choose one or the other? Answer: he has one. The whole purpose of software development is to do something. No matter how you get there? Depends. If you get there very carelessly and require a lot of updates, this is a big waste of time. Does it program? Absolutely not.
It seems your real question is how to integrate OO into the website. This is really not what you should do for this. What are you trying to achieve with this website? It contains many different states of an application with user interaction in FSM or it is just a bunch of static pages. How much information does these pages share?
First consider your specific questions:
- What types of classes are usually created? Whatever class he wants! Before entering a single character code, you must sit down and write a project for your application. No one can tell you how to do this: write some documentation, pseudo-code (even in php, if you want), draw up a UML diagram, etc. Find out how your application can be shared. Do not make object division errors based on their logical separation in the real world (User, Book), if that makes no sense. Why do you need a book object? How does it interact with the user?
You can create a very simple class to display a standard web page:
class anypage { public function header() { return '<head></head>'; } public function footer() { return '<br />Copyright © ' . date('Y'); } public function render($body) { return '<html>' . $this->header() . "<body>$body{$this->footer()}</body></html>"; } }
Then to any page call:
$page = anypage; echo $page->render("My Page Content");
In any case, I'm talking about the general principles of OO than about PHP, but this is a question that cannot be answered. You develop the classes that your application requires to do the job. A good example of using an object in PHP is PHPTAL , a template language. Your web page might look like this:
$page = new PHPTAL('mytemplate.xhtml'); echo $page->execute();
The PHPTAL class takes care of creating html from a template file. This is an example of which object you can use for.
Of course, PHPTAL is huge, and it is divided into many compartments. Looking at the source code, we see the classes Attr, Element, Node .. all these are fragments of an XML document, and they have a class representing them.
An important part of OOP is that objects work together. Each object should serve a purpose and do what is good. I do not like objects that are models for nouns, such as "User" or "Book", if they do nothing. PHPTAL does a great job of presenting a view for your page, but how do you know which page to display? You can use an object to handle this:
class controller { public function __call($_, $_) { return $this->hello(); } public function hello() { session_start(); $view = new PHPTAL('hello.xhtml'); return $view->execute(); } public function goodbye() { session_destroy(); $_SESSION = array(); $view = new PHPTAL('goodbye.xhtml'); return $view->execute(); } }
On the main page you would:
$c = new controller; echo $c->$_REQUEST['action']();
If the "action" is set by the request, the controller responds accordingly, calling this action. hello used by default thanks to magic __call() .
Like other OO languages ​​you are familiar with, you can even have events in PHP:
class controller { private $models = array(); private $_listeners = array(); public function __construct() { $model = new model; $this->models[] = $model; $this->_listeners[] = $model; } public function action($action = 'hello', $events = array()) { $this->$action(); foreach ($events as $type => $data) { foreach ($this->_listeners as $listener) { $event = "fire_" . $type; $listener->$event($data); } } } } class model { public function fire_hello($name) { echo 'hello ' . $name; } }
Like the other answers, a good start is to take a look at the PHP framework. I recommend Kohana , as it is a good OO structure. I do not necessarily suggest you create a website, but look at the documentation and code to better understand the development of OO for PHP.
2 PHPTAL or another template engine is a great way to reduce html duplication. An example is even my miniature class anypage . You do not have to have an html file, and then use require() include() or virtual() to pass it into your code for reuse. Instead, you can have a wrapper that stores and creates a common html template. I cannot stress enough how much I love PHPTAL for this purpose. XSLT is an alternative.
3 There is no way to do this. Others suggest using the framework. I do not necessarily disagree. Using the framework helps a lot in taking care of some unpleasant things to do with customization. This is not a magical problem solving a placebo, but it is good for some things. If you are just trying to learn and there are no limits in time, I say, I am writing your own framework. This will definitely teach you how to program OO in PHP. If time is a concern, use one of the 500,000 frameworks that others offer. Regarding the structure of your website, I usually have a _js folder for javascript and a _stylesheets folder for stylesheets. All the php and xhtml template files that I use are simply placed in the folders that they belong to logically. This is not rocket science, this is software development!