tags:

views:

74

answers:

6

Hi,

I have been writing a lot of code for work in PHP/MySQL. So far it has all been procedural making use of functions for functionality occuring multiple times/places. Starting to find some of the site hard to manage - time to go OO.

I want to learn about MVC with object oriented PHP & MySQL. I have some experience in Java and MVC but never anything to do with web technologies, i.e. HTML/CSS/JS etc. I don't really understand how the dynamically generated HTML fits in with the classes etc.

I am after some recommndations about where I can start. Ideally some sites with great examples from the ground up. I don't really want to use a framework at this point because I find that it does too much for you. Once I understand the OO approach with MVC I'll probably use a framework to managei easier.

Cheers, Evan

+2  A: 

You have a lot of code, and despite being hard to change, it probably works. Making an overhaul is going to set you back a while, so the best approach is incremental. Find some ONE thing that would seriously benefit from using objects, and use objects there. Refactor as you are able to run tests. You can work this in with the ordinary flow of events, and things work out OK.

Frankly, if you're serious about removing redundancy and duplication, you'll often find places where just writing a small function can make a dent. If you do this often enough, you'll find groups of functions that work on the same data. That will suggest where to look for objects.

In other words, if you listen with the right kind of ears, the code will tell you.

Ian
A: 

If you just want a good online course, maybe you should have a look at

http://www.lynda.com/home/DisplayCourse.aspx?lpk2=653&srchtrk=index%3A1%0Alinktypeid%3A2%0Aq%3APHP%20Object%20Oriented%0Apage%3A1%0As%3Arelevance%0Asa%3Atrue%0Aproducttypeid%3A2%0Acategory_facet%3APHP

This course was really useful for a friend of me. The progress he booked when he finished this tutorial was really great. It takes you through the basics of building a CMS in PHP and object oriented.

Stegeman
Thanks for the link. This looks great, very detailed. Will have to give this program a try. I found that mostof the other CBT PHP/MySQL training was very procedural -- what I am trying to get away from.
evo
A: 

Not really an answer but too big to be a comment.

Actually HTML has nothing to so with classes and JS and CSS has nothing to do with PHP, MVC and OOP at all.

Your code should produce some data which is going to be displayed one or another way. You can use some class to render this data, but it's not that important class.

While JS and CSS are totally separate files usually, called by browser directly from the server, avoiding your application code (unless being generated dynamically). Anyway you should have not too much concern in it.

Although it's still hard and non-trivial task to tie classes hierarchy with such a discrete way of execution of a typical PHP application. Once wise man have said once, "If Windows were have to restart after each user's click, it were written completely different".

Col. Shrapnel
Thanks for the response.. Currently my code is concerned with data and the rendering code (html etc.). My php scipts get executed on the server and based on the user input dynamic content is generated. I am less concerned with CSS/JS as these tend to be static files sitting on the server and php is completed ignorant of them. But a lot of the HTML is generated via the PHP..I have a lot of print "<html.." statements.. How do I avoid this in dynamically generated sites? Hence my MVC question.
evo
@evo Strictly speaking, nothing bad in these print "<html.." statements. That's just a way of printing data, one of may. There are better ways - yes, but the point is different. Your view component should call a template and populate it with data from model. There are many ways to make such a template. it could be a php file with lot of print "<html.." statements.. or an XSLT file. or a Smarty file. Personally I prefer a PHP file which just included by View when all data gets ready. Looks like this http://stackoverflow.com/questions/2574424/ The main point is to make all output in one place
Col. Shrapnel
Actually you can find many questions here on SO, related to templates. Here is another example of what I am using, http://stackoverflow.com/questions/3140714/ as well as other approaches
Col. Shrapnel
A: 

I think you should start with small pieces to get into OOP step by step. I mean that you should write simple classes for things you use often like handling forms, image uploads, site messages, session handling.

When you get used to programming classes and working on objects it will be time to jump for something bigger like refactor whole "engine" to OOP using mvc and other stuff.

killer_PL
A: 

Hello my friend, welcome to SO.

I am by no means a PHP expert, here is my thought comes on top of my head:

OO programming with PHP does help, but not very likely as the rescue to removing all the redundancies or making your code "neater". The traditional way of coding in PHP can produce nice code. OO helps to encapsulate your code to hide the implementation and reuse others' code by direct composition and/or via inheritance.

I would say the real concern here is how to decouple the "static" html from the "dynamic" PHP. For instance, there shouldn't be many :

echo "<h1>A title</h1";
echo "<p>blah blah blah...";

Even in conditional printing.

I would say separate the whole business logic on one page/script into several functions, and at least hide the nitty-gritty inside a function.

For your questions on popular OO frameworks of PHP, CakePHP is a good one, got its inspiration from Ruby on Rails.

Michael Mao
Hi Mat,This is exactly what I am trying to avoid.. I have way to many print "<html> goes here</htll>";What are some good strategies to avoid this when you are dynamically generating the page from a database?
evo
A: 

Read MVC Tutorial in PHP to understand what MVC is then start using a MVC Framework like Zend, CakePHP, Symphony or CodeIgniter...

php html