Learning OOP for PHP5

liunx

Guest
I am trying to wrap my head around OOP, doubt I am ready for it but hey! worth a shot. I am currently on the hudzilla.org web site reading up on poodles. Anyone know of some practical, easy to understand yet detailed tutorials and/or books on this interesting subject?

Thank you for any assistance you sexy space bunnies ;)This article (<!-- m --><a class="postlink" href="http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html">http://www.javaworld.com/javaworld/jw-1 ... iques.html</a><!-- m -->) gave me a good understanding of interfaces. It's written for Java but the same principle applies to PHP 5.I've had the same issues. My probelm was, while understanding the how of OOP (i.e. the syntax is easy enough to learn from php.net and some of the basic OOP tutorials on the net), I couldn't really wrap my mind around the why . What is its usefulness and how to best apply it.

The biggest breakthrough for me was when I started learning about design patterns. This is the first site that helped me in my understanding:
<!-- m --><a class="postlink" href="http://www.phppatterns.com/index.php/article/archive/1/">http://www.phppatterns.com/index.php/article/archive/1/</a><!-- m -->
He has some great articles that explain the usefullness of database accessors, factory classes, and the all important MVC pattern.

After that, I started reading more on n-tier applications and design patterns in general. Most of the stuff that's out there regarding design patterns is written for C++ and/or Java, but the concepts are all still the same. And now that the PHP syntax is resembling more and more an actual OO language with try/catch blocks, abstract class and method declarations, interfaces, and now, in the new version, a built in database abstraction layer (yes I realize it technically still isn't an OO language), its even easier to apply code written for Java to PHP.I'm currently reading Professional PHP5 (Wrox). It's doing a pretty decent job so far.I found that the hardest thing was changing over!

The biggest bit of advice I can say is be patient. Initially you may find yourself saying that it is a waste of time, but I can tell you know that I would not go back to procedural programming.

I would suggest that if you are starting a new project then try and do it in OOP. But only if you have the time to play around a little. (If you have a strict deadline you may get a little jaded with OOP. ) The easiest way to learn is by doing.That is the second time I have heard someone say they would never go back to procedural programming. I am about to re-code a CMS I wrote not too long ago. I have learnt a lot since so I would like to 'better' the way it works. Maybe this is the perfect project to start on. What are the major differences of how OOP works compared to PP? Can anyone give me a short and sweet example of code?One of my more favourite articles is this one on Sitepoint, PHP5: Comming to a webserver near you (<!-- m --><a class="postlink" href="http://www.sitepoint.com/article/coming-soon-webserver-near">http://www.sitepoint.com/article/coming ... erver-near</a><!-- m -->) .

While it is quite generic and an overview of PHP5, it has some nice examples of OOP patterns.

Also, all of the OOP articles here are quite a good starter.I find that I usually have a mixture of objects and procedural code. You may often find yourself wondering if objects are really necessary for the task at hand. A good example is when writing HTML templates with intermingled PHP. In that scenario straightforward procedural PHP is more appropriate (even if the template data is generated using OO techniques).

Remember that PHP can use both objects and procedual code at the same time. Use that benefit to your advantage.

Objects seem like a great idea but they can and are misused frequently. Most posts on this forum about so called OO code are actually procedural code stuffed inside of class methods for no good reason at all. Good use of objects requires some forward planning and discipline.... What are the major differences of how OOP works compared to PP? Can anyone give me a short and sweet example of code?

One of the most useful uses of OOP (at least in my opinion) is database abstraction. Say you have a simple program that pulls information from a database and displays the information on a table. in procedural code, your program is pretty much limited to one type of database. eg:

$result = mysql_query('SELECT * FROM a_table');
while ($row = mysql_fetch_assoc($result)) {
echo "<td>". $row['name'] ."</td>";
}
mysql_close();


Here's the same table using the PEAR DB object.

$query = "SELECT * FROM a_table";
$result = $db->query($query);
if (!DB::ERROR($result)) {
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
echo "<td>" . $row['name'] . "</td>";
}
}


Notice no references to mysql or any other type of databse. Now your code is portable to a number of databases without changing the code at all.Being a bit nitpicky today but wouldnt it be $db->fetchRow or do you create objects for each method? Anyway you really should read up on Java and OO i havent yet read the link that Shrike gave earlier. Unfortunately with PHP 5 still you cannot utilize all the concepts of OO within PHP first thing to mind is Method Overloading something I use a lot with Java.It would be very hard to implement method overloading directly in PHP, because you'd really rather have strong typing to do it. (Then again, strong typing would sometimes be nice in PHP.) What I do is have the one method with an empty calling signature, and use func_get_args() to get what was passed and branch on that.The best way to learn OOP is in a programming class. If that is not an option then I would first start off 'using' the simple concepts then working your way up. First list out all your functions of a project and learn how to group them properly into classes. Then learn how to create and use objects. After that you should learn inherentence, encapsulation, and accessor methods. I would then stop learning and use what you learned above on an application or two.

Only after you are fully understand classes and objects should you start to read and learn things like Interfaces and Patterns. If you try to learn it all at once more likely then not you will get confused.

And before OOP I highly suggest you learn UML. You should have UML documentation created before you start building any OOP site.Being a bit nitpicky today but wouldnt it be $db->fetchRow or do you create objects for each method?

Depends which DB Abstraction you're using. In PEAR, the result returned from $db->query() is an object, unless the SQL is manipulation (i.e. INSERT, DELETE, UPDATE, etc.).

Only after you are fully understand classes and objects should you start to read and learn things like Interfaces and Patterns. If you try to learn it all at once more likely then not you will get confused.


I have to say, in my personal experience, I've found the opposite to be true. It's easy to understand the concepts of grouping functions together and calling it a class. If you can learn procedural programming, learning how to write a class is easy. Of course encapsulation and inheritence are key to OOP, but it didn't really click with me until I started reading up on design patterns and interfaces. After I learned how to write a DAO (database abstraction object), I realized how useful that object could be. The MVC pattern helped me realize the importance of separation between the code and the output.
Its easy to learn how to make a class, just by reading online tutorials and the php manual. Design Patterns are what make classes useful and reusable, which is the ultimate goal of OOP in the first place.
 
Back
Top