Starting out with OOP: recommended reading?

I’ve been using PHP for several years, but have not tried OOP at all. I’ve read about it, but never got to grips. I think my problem is that although I understand the English when I read it, the concepts remain abstract and my old brain doesn’t retain it too well.

I have seen suggestions that using a Framework would solve my problem, but I don’t want to use something I don’t understand.

Can anyone recommend a book that starts fairly gently, but uses actual (initially simple) examples (that I can work with on my computer) to try to get the stuff to stick in my head ? The book needs to be PHP specific, because I don’t do other languages.

-> is essentially punctuation. You don’t read commas or semi-colons – unless giving verbal dictations. It’s much easier to read user addName John than “pass ‘John’ to the ‘addName’ method of the ‘user’ object.”

Thank you, Gavin. I’m sure that’s a useful piece of code, but until I’ve done some basic reading it makes little sense to me.

The original purpose of this thread was to get some recommendations for books to introduce me to OOP. I think that’s been achieved, and I’ve placed my order.

I’m sure that once I start reading I’ll be back again with detailed queries about coding OOP. Here’s one for starters:

We can say ‘==’ is ‘equals’ when reading code, but how do you say ‘->’ ?

php anthology
http://www.sitepoint.com/books/phpant2/

Object Oriented Versus Procedural Programming In PHP

I had a VERY hard time grasping OOP but it is WELL Worth it once you start understanding it. I never read the PHP Anthology, but I have some books that were too advanced for me as a beginner so I wouldnt recommend them.

I would definately look for a book like suggested above that compares Procedural vs OOP because it’s really a total different way of thinking that has to shift.

check out for lynda php video tutorials, I have found them usefull in understanding oop as well as computer sciense class of Mehran Sahami (professor at Stanford) can be found on youtube, although this class uses java syntax I think it’s worth of your time.

Thank you both. I will follow up.

I’ve already got the PHP Anthology, so I’ll start there. I’d much prefer a printed book - I only have one screen on my computer - any chance, Sitepoint ? (I baulk at printing 500+ pages)

Amazon UK still sell the printed version (and is cheaper and quicker delivery then SitePoint), when I check a moment ago they had just 1 left in stock.

Thank you, that’s good news. The Sitepoint web site says “Instant Download…This title available in Adobe PDF format only”) and I thought they should know. Perhap there’s an added chapter, or something…

It also took me a while to get into OOP. At first, I started encapsulating functionality (more like groups of related functions) into classes. So essentially, I was still writing procedural code. Moving away from a procedural way of thinking, and getting use to have a hierarchy of objects for handling application flow, does take time.

I’m actually yet to write an application written in OOP, but that’s essentially what I’m doing now as I’m working on my own framework. Progress is slow as for each step I take, I make sure I’ve researched most if not all possible solutions, to ensure that the final product is as flawless as possible.

I’m pretty much a collector of books… I Get just about every book published on PHP. Packt Publishings Learning PHP Data Objects, Apress OPHP Objects Patterns and Practice, and PHP Architects Guide to PHP Design Patterns are really nice for starters.

In my opinion I made a mistake not paying too much attention to OOD and instead started hacking away with OOP concepts. I’ve realized over years that developing a sound understanding of OOD will go a long way compared to hacking OOP. I personally recommend Matt Zandstra’s PHP 5 Objects, Patterns and Practice. You might also want to give php|architect’s guide to design patterns. I’m in the middle of reading Matt’s book and love it!

Thanks, everyone.

Matt Zandstra certainly gets good reviews on Amazon, so I think that’s going to be my start-point (given that I already have the latest ‘PHP Anthology’ from Sitepoint as a PDF).

Amazon also offer ‘Practical Web 2.0 Applications with PHP’ by Quentin Zervaas (also from APress) which sounds (from the reviews) like a good source of code to use (and understand too, I hope). Anyone tried that one ?

PHP Objects, Patterns, and Practice by Matt Zandstra is a VERY good book. It gets tough to break in after the first few chapters but it covers a LOT, I’ve been reading that book for about 5 months now.

It does not need to be PHP specific, it need to appeal and address you using techniques and examples you grasp, C# and Java code examples are very easy to equate to PHP since the advent of PHP5.

BTW, look at “Head First Design Patterns” - catchline “Your brain on design patterns” - if like me you need to have it all spelled out in an easy to understand, almost graphical, and some would say, child-like way. [expensive]

It worked for me.

You have to accept that you will end up buying more than one book, and the PHPArch Guide to PHP Design Patterns is a good companion book. [cheap]

Best thing is to swim to the mainland, hitch a ride to a big town, find the biggest bookshop and skim-read as many books as you can, then just steal them and run away fast.

Thank you, Cups. I too always end up with several books, most of which get read. Your solution is a bit drastic. Unfortunately I fear it would make the books a bit soggy. The best book-shop in Glasgow just went into receivership, probably because we all went in there to browse, and then ordered from Amazon…

This thread is very good:

Yes, it’s very good (thought not all easy reading), even though it’s also seven years old. I just wonder if they had PHP 5 readily available that long ago ?

You’ll always hear KISS.

In the simplest form, you can create classes that replicate a row in a table in your database.


<?

    class User
    {
        private $UserID = null;
        private $Username;
        private $Password;
        private $Forename;
        private $Surname;

        public function __construct($Data = null)
        {
            // if you provide an associated array of values
            // as a parameter, it will attempt to set the object's
            // properties. Of course, the names have to match
            if(!empty($Data))
            {
                foreach($Data as $Key => $Value)
                {
                    if(property_exists($this, $Key))
                        $this->$Key = $Value;
                }
            }
        }

        public function __toString()
        {
            // output the user's name if we 
            // try and echo the object
            return sprintf('&#37;s %s', $this->Forename, $this->Surname);
        }

        public function Save()
        {
        }

        public function Delete()
        {
        }
    }

?>

The above, is a sample of one of my classes. The class replicates the schema of a sample User table. It has the ability to create a new instance with no data, of if you wish, you can pass an associated array (from mysql_fetch_assoc) as a parameter, which will fill the object for you.

The object then has the ability expose functions that will affect the data in that object directly.

This allows me to create a Save and Delete function. The Save function, would determine whether the UniqueID ($UserID) is null or not. if it is null, it is a new row.

Then, if its a new row, it will insert, otherwise update.

Likewise, delete, will delete the row, if it already exists in the database.

Once you grasp the simple concepts like above, you can expand on it.

hth

Gav

In PHP, -> references a Property or Method within a class. There is probably a more known term for it, but at the moment, I don’t actually know it.

Fill’s you with confidence doesn’t it :wink:

So you could use


$InstanceOfObject->PropertyOfObject = 'Value';

or


if($InstanceOfObject->PropertyOfObject == 'Value') {}

or


$InstanceOfObject->MethodOfObject();