views:

151

answers:

6

Hi

I am working with PHP for last 1.5+ years with procedural oriented programming. I like to move from procedural oriented code to object oriented code.

But i am struggling with this. I don't know how to start and where to start. I having basic knowledge of OOP. But i don't have any idea on how to implement that.

I hope most of the persons might cross this situation.

So friends, I need your valuable suggestions (it may be any type) to improve my level of code.

Thanks in advance.

A: 

If an object only contains data members, it shouldn't be an object (usually). You should encapsulate data members along with the methods (functions) that perform actions on those members.

For example: a Customer object might have a _firstName and and _lastName, and also the methods Delete() and Add().

There's obviously much more, but I just thought I throw that 2 cents in.

JohnB
+1  A: 

I'd recommend finding some tutorials. Googling "object oriented php tutorial" should get you some results, like this link:

http://www.killerphp.com/tutorials/object-oriented-php/

If you understand the basic theory of OOP, it shouldn't be too hard to figure out how to do it in PHP.

Ryan Kinal
+2  A: 

I've been there and I can tell the easiest and fastest route is:

  1. Learn how to use Classes and Objects
  2. Learn how to build applications with an MVC framework

Skipping the second step because "I want to do it all by myself" won't get you anywhere, I can tell from experience, you'll either lose interest because you never get it no matter how much you read on the subject or you produce bad code and waste your time. MVC frameworks provide you with a complete framework built around used and tested design patterns implemented with PHP's OOP features. As you code with an MVC framework you'll learn how to use these design patterns and you'll learn how designing OOP applications in PHP works.

There are a lot of MVC frameworks available, you can find recommendations and arguments for and against each of them here, just search. Personally I tried many, but the one I found the easiest and where I immediately "got it" was the Kohana framework.

TheMagician
+1 for learning from existing code. Might I also suggest a peek into design patterns? It helps to be able to put a name to the mechanisms you see in a OO framework
Manos Dilaverakis
@Manos Dilaverakis: Absolutely, in fact I thought I added it, at least initially I included it to the post, but then I changed it a bit while still writing the reply.
TheMagician
A: 

I would recommend CodeIgniter framework, it is classes, objects and methods all the way! It provides vast documentation and tutorials that are extremely helpful at the time of understanding how encapsulation works.

I am also in the transition from procedural to object oriented programming. This framework has been a powerful tool in the learning process.

A way of thinking about OOP that helped me (I am not saying is theoretically right) is that in some way on procedural programming we use some OOP concepts. We have built-in data types on each language, "classes", and each data type has specific built-in functions, "methods", that operates on them.

The core concept of OOP is create classes, which are data with some attributes defined by the programmer and assign methods to them. This proccess is what we call encapsulation, putting some data and functions in one package.

jromero
He wants to learn OOP not a framework, especially not one that is still compatible with PHP4
Gordon
+2  A: 

You seem to be in a similar situation as I was a few years ago.

I knew how OOP worked in theory, had seen all the classic examples (class Dog extends Animal, etc), but had no idea how to actually put that into something useful.

What really opened my eyes was the concept of a Record class that corresponds to a row in a database table. I'd worked with databases quite a lot and my code was always an unorganized mess of basic SELECT/INSERT/UPDATE queries that was hard to maintain and only partially safe against SQL injections.

With a Record class, it might go something like this:

Assume you've got a table called testTable with multiple columns including id and quantity.

// Creates the object, runs a 'SELECT * from testTable WHERE id = 123' query
// and stores the returned row in an array inside the object  
$record = new Record('testTable', 123);

// Let's say the quantity column was 3 where id was 123.
// This would set $quantity to 3.
$quantity = $record->getField('quantity');

$newQuantity = $quantity + 1;
$record->setField('quantity', $newQuantity);

// Runs an 'UPDATE testTable SET quantity = 4 WHERE id = 123' query
$record->save();

And then possible improvements to consider:

  • Make the ID parameter in the constructor optional. If ID is not provided, it would skip the SELECT query and do an INSERT query in save() later on.
    • Add an isNew() method which returns true if the row doesn't exist in the database yet.
    • Add a getID() method which would start returning the inserted ID after save() has been run.
  • If you haven't already, do all SQL injection safety checking inside the Record class so you'd never have to worry about it when using the class.
  • Make the Record class implement ArrayAccess, so that instead of
    $quantity = $record->getField('quantity'); and
    $record->setField('quantity', $newQuantity);
    you could do
    $quantity = $record['quantity']; and
    $record['quantity'] = $newQuantity;
  • Extend the record class as TestTableRecord:
    • Override the constructor to only require the ID parameter and pass the 'testTable' to the parent version (ex: parent::__construct('testTable', $id);)
    • Add an increaseQuantity() method to the extended class.

Some may want to point out that similar classes already exist in common PHP OOP frameworks and you shouldn't need to reinvent it. But for learning purposes, I'd highly recommend going through the process at least once. Otherwise it can be very difficult to understand those frameworks at first.

mjomble
thanks for your reply mjomble. But could you provide me some example links or books to refer. I agree with you regarding starting from the base. But according to my capability to be frank i need some good reference. Will u please provide me that... Thanks in advance
Fero
I personally first learned OOP in Java at university and then later "ported" my knowledge to PHP. So I don't really know of any particularly good learning materials. It might be best to combine other people's links with the above exercise task.
mjomble
A: 

Recently I found a book that I think could fit your learning needs. It's called Object-Oriented PHP: Concept, Techniques and Code, by No Starch Press. It's pretty straight forward, provides code examples, some background about PHP moving to OO paradigm and wide explanations about concepts.

jromero