OO or procedural PHP for website?
I'm about to begin work on my first ever PHP project -- building a new website for a small non-profit organization. Coming from a .Net and Java background, object oriented programming comes very naturally for me, but I'm not sure if it's the right approach for bulding a webs开发者_如何学Pythonite of moderate complexity in PHP. My understanding is that most PHP-based sites are written mostly in non-OO code.
For a web application I would definitely go the OO-route, but for a fairly simple website, I'm not so sure. The site will consist of about five sections, with one to four content pages per section, containing articles, news, image galleries, and a few forms. There is no complex interaction involved (except for a few fairly simple web forms for writing articles, comments, registering, etc), and no need to maintain state (except for logins). MySQL will be used for data storage.
The code does not really need to be particularly extensible as such -- this is not an enterprise website or a templating engine we're talking about -- but it is important that the code is fairly easy to understand for a programmer with a decent amount of PHP experience. I'm guessing most PHP-programmers are not used to OO-code, so perhaps this is one point in favor of procedural code?
One aspect in favor of OO is that there will be different types of articles that are, in the database level, based on a supertype containing most properties, which of course transaltes very naturally to OO-code.
Perhaps some kind of hybrid approach would be best, using objects for representing the "business objects" etc, but rendering HTML etc using traditional, procedural code?
Comments greatly appreciated. --Rolf
but it is important that the code is fairly easy to understand for a programmer with a decent amount of PHP experience.
Readability of code is not tied to a programming paradigm but to how the code is written. I have seen my fair share of spaghetti OOP (including my own) and I have seen an equal amount of procedural messes. If the code is well written, even someone without a decent amount of PHP knowledge should be able to follow along.
I'm guessing most PHP-programmers are not used to OO-code, so perhaps this is one point in favor of procedural code?
I doubt this. I've been to a number of conferences and no one there seemed to have any problems with OOP. In fact, I didnt even see a single line of procedural code. Also, all of the major frameworks are full OOP. You will find the procedural paradigm mainly in PHP4 applications and when looking at rookie code.
But to answer your question: I'd say use OO if that is what you and your developers are comfortable with. Personally, I find procedural code in the View part a bad idea, because you will likely end up intermingling logic and presentation code for completely unmaintainable templates. See the POEAA's Web Presentation Patterns for some more maintainable approaches.
You don't have to use MVC if you feel its too oversized. Use a Page Controller if you like. But then again, MVC aint that hard to implement either and there is plenty frameworks out there that will take the brunt of work away from you.
The first thing to accept when writing PHP is that it is primarily a templating language. PHP is meant to be put in an HTML document to make bits of the document dynamic.
PHP is capable implementing OO designs, but the code you write will not be nearly as nice as similar C# code (and possibly Java, but I don't know Java to comment).
You say you'll have an interface for creating articles - that does sound more complex than simply adding a little dynamic content here or there. This might benefit from the OO treatment.
On the other hand, there are many PHP CMSs that are already made, such as wordpress, Drupal and Joomla!, that might suit your needs out of the box.
In conclusion - if a pre-made solution doesn't suit you, go the OO route with some procedural script to tie it all together.
It all depends on your aim.
- If you do not wish to extend your project later on a procedural approach is ok.
If you are able to develop part of your code faster using predefined objects then that is the way to go.
If can you just write down your code from top to bottom without much effort "procedural" is fine too.
I would like to recommend WordPress, over writing code from scratch for fairly simple site.
All you need to webspace and little time to learn how to customize wp.
Check out the showcase
If you still want to write from scratch.. its better to follow OO, may be you can use frameworks like Zend or Kohana
Cheers
The popular myth that procedural is not extensible is absolute BS. If you really want a live example, then have a look at linux kernel developement in github. There is no doubt its one of the most complex projects in the world and its community driven from day one. So, the fact that C is completely structured and procedural in nature doesn't limits it extensibility. Also, to point out that OOP is basically a concept that is emulated in php. At low level its ultimately flattened. Keep your logics seperated and alwasy make reusable functions. Focus on user inputs and sanitize data. Focus on database design strongly. As linus once said "Bad programmers focus on code, good programmers focus on data structure".
I would definitely go with an OO approach.
You can use Zend Framework, sports both an MVC and a persistence framework. Or you could use some more lightweight and specialized frameworks like doctrine for persistence and i believe (but i'm not sure) cakephp is pretty lightweight and good for MVC.
Not only maintainability increases, but this way you also cut development time, the way I see it it's a win-win situation!
To be fair any and every website created in PHP should be created in a OO-way. As you've also programmed .NET and Java you should know that simply coding in OO makes the code maintainable. Also, it's good practice if you ever plan to create more websites using PHP.
Personally, I consider PHP coders who aren't well versed in OOP incompetent and/or well behind the times - that shouldn't be your reason for sticking to procedural, at least unless the client will want to change little touches of it but is only atall confident with procedural code.
The major advantage of OOP as I see it for PHP is that it encourages both fairly neat coding styles like MVC (or similar) and encourages coders to use & build libraries for common tasks - which means you tend to get a cleaner, easier to maintain solution in much less time and likely with fewer undiscovered bugs.
If it's a simple website, and the content is not going to change that much, then I would not frown upon someone using procedural code over writing a full, object-orientated solution (which may be overkill). For example:
<?php
switch ($_GET['filename']) {
case "news":
require_once('inc/news.php'); // your news functions
include_once('tpl/news.tpl.php'); // your news template
break;
case "events":
require_once('inc/events.php');
include_once('tpl/events.tpl.php');
break;
case "contact":
require_once('inc/contact.php');
include_once('tpl/contact.tpl.php');
break;
default:
if ($_GET['filename']=="") {
include_once('tpl/home.tpl.php');
}
else {
include_once('tpl/page_not_found.tpl.php');
}
break;
}
?>
The above, in a file called index.php
, would act as a simple controller. For small-scale sites, I use a similar—albeit slightly more complicated—setup. I usually have "business login" in the files in the inc
directory (for example inc/news.php
would contain a class with methods for fetching news articles etc.) and then template files within a tpl
directory.
Not object-orientated, but not bad either. It frustrates me, people who will swear blind that if it's not object-orientated then it must be bad code.
Use OO for your infrastructure: controllers, services, facades, etc
For data transfer go with arrays
<php
class Controller {
private $message;
function build($data) {
$this->message = $data['word1'] . $data['word2'];
return $this;
}
function echo() {
echo $this->message;
return $this;
}
}
(new Controller())->build(['word1'=>'Hello', 'word2'=>'World'])->echo();
I'd go with oop. It will be easier and you will have the rewrite less code in case you chance something :)
Definitely go with OO.
How small your website will be, it can always better be modified/extended using OO.
Also I can recommend a very good and speedy framework:
http://www.yiiframework.com/
Totally in PHP5 and OO :)
精彩评论