evo
evo

Reputation: 21

Help getting started with OO PHP & MySQL

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

Upvotes: 2

Views: 550

Answers (6)

adiian
adiian

Reputation: 1392

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

Upvotes: 0

Michael Mao
Michael Mao

Reputation: 10168

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.

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157872

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".

Upvotes: 0

Piotr M&#252;ller
Piotr M&#252;ller

Reputation: 5558

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.

Upvotes: 0

Rene Terstegen
Rene Terstegen

Reputation: 8046

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.

Upvotes: 0

Beth
Beth

Reputation: 4670

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.

Upvotes: 2

Related Questions