Book Review: Object-Orientated Programming with PHP5 (Hasin Hayder, Packt Publishing)

After an email out of the blue from someone at Packt publishing, here's a review of "Object-Orientated Programming with PHP5"

I don't think I've done a book review before, so apologies in advance if it's not structured in any logical manner.

Contents

Briefly summarised, the book's chapters are :

  1. OOP vs Procedural Programming (what is OOP?, why?, differences between etc)
  2. Kick Starting OOP (objects, inheritance, polymorphism etc)
  3. More OOP (useful PHP functions, exceptions, iterators etc)
  4. Design Patterns (Strategy, Singleton, Adapter, Observer, Decorator etc)
  5. Reflection and Unit Testing (phpUnit)
  6. Standard PHP Library
  7. Databases in an OOP way (MySQLi, PDO, Abstraction layers - PEAR::MDB2, ADODB and Active Record)
  8. Cooking XML with OOP (SimpleXML, xpath, Dom)
  9. MVC / Frameworks

It's aimed at beginner/intermediate programmers

Pros

  • Introduces SPL (Standard PHP Library) in reasonable depth
  • Introduces PDO, PEAR::MDB2, AdoDB and MySQLi
  • Introduces the main OO patterns
  • Introduces MVC and frameworks

Cons

  • It's only 250 pages
  • It doesn't go into much depth in any particular area
  • I seemed to notice plenty of grammatical errors in the book - often missing pluralisations. I'm no grammatician, and some would argue in no position to lecture... but even so, somehow I expect published books to be near perfect.
  • There were occasional spelling mistakes in the code (e.g. s/Mehod()/Method())
  • Some of the example code was poorly presented/written (see below for example)
  • 10 pages are spent providing the PHPUnit API, and the last chapter focuses on what I assume is the author's home made framework. I feel the pages would have been better spent covering one of the main frameworks (e.g. Zend or CodeIgnitor). Having said that, I found it vaguely interesting to see how someone else had tackled the problem - but I doubt the book's target market of beginners/intermediate programmers will.

Random Code Example

To give a better flavour of the book, chapter 4 covers various software patterns. One of which is the decorator pattern. Part of the example shown is as follows :

$post = new Post();
$comment = new Comment();
$post->filter();
$comment->filter();
if($BBCodeEnabled==false && $EmoticonEnabled==false) {
    $PostContent = $post->getContent();
    $CommentContent = $comment->getContent();
}
elseif($BBCodeEnabled==true && $EmoticonEnabled==false) {
    $bb = new BBCodeParser($post);
    $PostContent = $bb->getContent();
    $bb = new BBCodeParser($comment);
    $CommentContent = $bb->getContent();
}
elseif($BBCodeEnabled==false && $EmoticonEnabled==true){ 
    $em = new EmoticonParser($post);
    // etc.
}

I feel this would have been better written as :

$post = new Post();
$post->filter();
$comment = new Comment();
$comment->filter();

if($BBCodeEnabled==true) {
    $post = new BBCodeParser($post);
    $comment = new BBCodeParser($comment);
}
if($EmoticonEnabled==true){
    $post = new EmoticonParser($post);
    $comment = new EmoticonParser($comment);
}
$PostContent = $post->getContent();
$CommentContent = $comment->getContent();

But I digress....

Summary

As you might see, I have slightly mixed feelings about the book. I think it grew on me as I read more of it, and some of my initial negativity wore off over time.

The book does provide a good grounding in OOP and Software Design Patterns. However, it doesn't really go into enough detail when it covers PDO, MySQLi or PEAR::MDB2 for it to be a usable self contained resource.

I think it would have been better for the book to drop the section on the home grown MVC framework, and the PHPUnit API and expand some of the examples. It doesn't cover what it does in enough depth on it's own - so don't buy it if you're looking to gain a working knowledge of something it covers (e.g. PDO, AdoDB or the like) from scratch.

The book does maintain something like a 50:50 ratio between code and text. At times this does make it feel a little like the author is padding the book with code, and duplicating the API at php.net for no real reason.

Disclaimer - I got the book for free, as long as I published a review.....

Technorati Tags:

comparing booleans for equality with true

if ($foo == true) feels redundant to me; if $foo is a boolean, why not just use if ($foo) ? Or is this some nuance of PHP which which I am not familiar?

if booleans

Hi

Yes, you can just do :

if($foo) {
    // then something
}

There's no need to do '==', but obviously '===' is required if you want to do a strict equality (i.e. same type or object instances)

David.

Re if() booleans

I tend to use the == true more often than not. The addtiaionl 7 characters make it a little easier to maintain over the long term. This is especially true for if(!$foo) where it's easy to miss then exclamation mark when going over someone else's code that hasn't been touched for a year.

Rob...

Spelling mistakes I can deal

Spelling mistakes I can deal with, but mistakes in the code.. well..

That just meant they didn't even try their own code before putting it on the page.. Bad sign, i'd say..

indeed

Yes; although the code I included in the review isn't broken - it's just not as clean/nice as it could be.

As far as I could tell there were no errors in the code, although I've not tried running any of it. The worst was incorrect spelling (although consistency was maintained by the author's IDE) and inconsistent indentation (presumably tabs vs spaces?)

OrientATed?

Well, the biggest spelling mistake is in the title; why do they name it "orientated". It's "oriented"...

ah.. that's probably me

I don't have the book at hand, but that's one of those typos I keep making over and over and over again ....

David.

hi, i have to say that i

hi,

i have to say that i found the mvc part very interesting, certainly nothing for beginners,
but otherwise not bad...

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is used to make sure you are a human visitor and to prevent spam submissions.
2 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.