PHP, OOP and MVC...any opinions?
Moderator: General Moderators
-
Zander1983
- Forum Newbie
- Posts: 20
- Joined: Mon Mar 21, 2011 2:26 pm
PHP, OOP and MVC...any opinions?
Hi
For years, whenever I've written PHP, I've not bothered with classes and object oriented programming in general. In .NET, I always have though. I was wondering,do most people use OOP principles with modern PHP sites? How about the MVC model? Do people use these principles? What are the advanatges to following these methods?
I've been reading these articles on MVC and DAO: http://www.phppatterns.com/docs/design/ ... er_pattern
http://www.phppatterns.com/docs/design/ ... re_widgets
They've left me with the impression that I need to change my PHP ways and write more principled code...any opinions?
Thanks
Mark
For years, whenever I've written PHP, I've not bothered with classes and object oriented programming in general. In .NET, I always have though. I was wondering,do most people use OOP principles with modern PHP sites? How about the MVC model? Do people use these principles? What are the advanatges to following these methods?
I've been reading these articles on MVC and DAO: http://www.phppatterns.com/docs/design/ ... er_pattern
http://www.phppatterns.com/docs/design/ ... re_widgets
They've left me with the impression that I need to change my PHP ways and write more principled code...any opinions?
Thanks
Mark
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP, OOP and MVC...any opinions?
Yes, you need to change your PHP ways and write more principled code.
(#10850)
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP, OOP and MVC...any opinions?
Yes, and there are many frameworks that can help tie all of this together for you, similar to .NET as a framework. Zend is a good framework for PHP, though I prefer CakePHP.Zander1983 wrote:Hi
For years, whenever I've written PHP, I've not bothered with classes and object oriented programming in general. In .NET, I always have though. I was wondering,do most people use OOP principles with modern PHP sites? How about the MVC model? Do people use these principles? What are the advanatges to following these methods?
I've been reading these articles on MVC and DAO: http://www.phppatterns.com/docs/design/ ... er_pattern
http://www.phppatterns.com/docs/design/ ... re_widgets
They've left me with the impression that I need to change my PHP ways and write more principled code...any opinions?
Thanks
Mark
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
Zander1983
- Forum Newbie
- Posts: 20
- Joined: Mon Mar 21, 2011 2:26 pm
Re: PHP, OOP and MVC...any opinions?
@AbraCadaver, ok thanks. I have a website that took 6 months to develop and is quite large. since im developing it further along with 2 other developers, any advice on how we can implement good practices on all this old code? Ill take a look at CakePHP and see can i import and straighten out the old code
Re: PHP, OOP and MVC...any opinions?
I just started playing with yii framework, and so far am liking it. I do love the MVC way of working, and while normally I don't break it completely apart to separate M V C, in my scripts I do like to try to keep it organized so that by the time you start <html>, everything is already loaded up into a $aryData (data) and $aryLists (lists of things, ie, States, Categories, etc), so the only PHP in the HTML secion is just echoing out values, foreach loops on data, and/or conditions to determine how to display it.
Also, while in the PHP chuck at the top, I'll use:
but once down in HTML, stick with:
I have a set if functions I use specific for these, such as echoHSC which just does echo htmlspecialchars($val); just to make the code above cleaner. (this is the only place I really miss short tags, the use of <?= $var ?> )
The more I'm playing with Yii, the more I am liking it and think I will change to use it for next main project. Definitely a learning curve, but I think it will be worth it. My big problem with OOP is having good examples to work off of to practically use it to the fullest. I get the ideas of it, but at times don't like throwing everything into a class just to make it a class. (Think it comes from my programming roots at programming a Commodore 64, where you had 64k to work with, so you optimized everything as much as possible)
-Greg
Also, while in the PHP chuck at the top, I'll use:
Code: Select all
foreach($var as $key=>$val) {
// do something....
}Code: Select all
<?php foreach($var as $key=>$val): ?>
<li>
<a href="/processme.php?id=<?php echo $key; ?>">Click here to read about <?php echoHSC($val); ?></a>
</li>
<?php endforeach; //foreach($var as $key=>$val) ?>The more I'm playing with Yii, the more I am liking it and think I will change to use it for next main project. Definitely a learning curve, but I think it will be worth it. My big problem with OOP is having good examples to work off of to practically use it to the fullest. I get the ideas of it, but at times don't like throwing everything into a class just to make it a class. (Think it comes from my programming roots at programming a Commodore 64, where you had 64k to work with, so you optimized everything as much as possible)
-Greg
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP, OOP and MVC...any opinions?
You may also want to look at Zend, Symfony, Lithium and CodeIgniter -- in addition to the frameworks mentioned above.
(#10850)
-
Zander1983
- Forum Newbie
- Posts: 20
- Joined: Mon Mar 21, 2011 2:26 pm
Re: PHP, OOP and MVC...any opinions?
@twinedev, thanks for input, ill have a look at yii.
@christopher, I generally use eclipse for php development.
So how many people actually use frameworks and how many code from scratch? is MVC really necessary and if so why?
@christopher, I generally use eclipse for php development.
So how many people actually use frameworks and how many code from scratch? is MVC really necessary and if so why?
Re: PHP, OOP and MVC...any opinions?
Whether or not I use a framework really depends on the size and complexity of the project at hand. Often it's nice to not have to write the same bits of code over and over (database abstraction, authentication, etc.), but sometimes the project is so small that the overhead simply isn't worth it.
I like MVC because it creates a nice separation of concerns, but I'm sure others with more experience can elaborate on the benefits of (H)MVC far better than I.
I like MVC because it creates a nice separation of concerns, but I'm sure others with more experience can elaborate on the benefits of (H)MVC far better than I.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP, OOP and MVC...any opinions?
I was just talking to social_experiment about this the other day. The thing about patterns is that they are not necessary in and of themselves -- they are solutions to specific problems. In fact, every pattern definition starts with a description of the problem. That's the whole point. They are not standard solutions, they are best practice solutions to common problems. I think it is an important distinction.Zander1983 wrote:So how many people actually use frameworks and how many code from scratch? is MVC really necessary and if so why?
For simpler patterns the problem and solution is clear. But MVC is a high level pattern and you need to work through the stack of problems that lead to the MVC solution. Web applications show this progression of problems well and why MVC is popular in web apps.
The simplest solution for a website is the Transaction Script pattern. That is what a web page that contains all the code in one script is. These are really handy for many simple problems and small websites. For example, a website might only have one PHP script -- maybe something that shows a list of items from a database. That script connects to the database, queries with SQL, fetches the records and generates HTML inserting the data. Very efficient and PHP make it super easy.
The next thing that happens is that several pages in the site need to display those items from the database. Maybe they are searchable; maybe they can be shown a category at a time; whatever. So typically PHP programmers pull out the code that fetches the records and put in in a separate script so that every page that needs it includes it. Hopefully there is a nice clean separation like putting that code in a class so that other code can accidentally change variables, etc. So now the application is divided into a Datasource layer and a Presentation layer. You could call that 2-Tier and it is probably the best single thing that any programmer can do when designing their application. The reason it is so good is that you have isolated the database from the presentation code, so database changes can be handled in a single place in that lower layer. You just change the SQL in one place and all the pages still work correctly.
So the next step up is to go to what is often called N-Tier with the most common implementation being Three-Tier. To do that we further separate our database access code into a lower Datasource layer and on top of that a middle Business Logic or Domain Model layer. Now the Datasource layer is just focused on getting and saving records from the database (or some other datasource). In PHP it could just be a PDO object, but probably it is an implementation of some Data Access pattern like ActiveRecord, Table Data Gateway or DataMapper. Business Logic or Domain Model layer sits on top of that Datasource layer and is designed to be an abstraction of what is referred to as the Domain of the problem. Typically that is classes like Users or Products that have methods that do the things that the application needs (see: Tell, Don't Ask).
In any sort of N-Tier architecture the Presentation layer is the topmost layer that is the direct interface to the user. N-Tier is a proven solution that has many benefits that make it easier to make changes to the application.
But even with N-Tier there are still sometimes problems. The main two are 1) being able to centralize the processing of inputs from the user to deal with things like Access Control and 2) getting some reuse from the code used to generate output. So MVC adds an additional separation by dividing the Presentation layer into an input and program flow side called the Controller and a output generation side called the View. In MVC the middle layer is called the Model, but really that means the various classes in the Domain Model layer. In MVC we tend to talk about the Controller, Models and View associated with a specific request, but really there are many Controller and View classes in the Presentation layer and may Model classes in the Domain Model. Since there are many Controllers, each associated with a specific set of requests, systems that implement MVC often also implement the Front Controller pattern which provides a single entry point for the application where the Controller is selected based on the "route" given.
(#10850)