PHP Master, chapter 1 (OOP)

Actually, I submitted this as an error, but maybe I better would have posted it here. At least nobody started off with calling me a monkey :eek:

I have a problem with the Courier and Parcel classes on one of the first pages in chapter 1.
In the constructor method of the Courier class it says: return true;
Now I would expect to have a boolean returned instead of an object, but in practice that is not true. You can also return false or ‘abacadabra’, it does not matter, but it gives the wrong message to newbie OOPs.

My second problem is the Parcel class. It has setCountry and setWeight methods, that return $this.
Why? From my Java background, setters should reuturn void (or NULL in PHP terms) and getters whatever you want to get.
Actually, I think, since there is a setter, there should also be a getter, and the properties weight and destinationcountry should be made protected.

Last comment: the autoload function says: strtolower for the filenames, but in my code archive, the names of the files start with uppercase. On my Windows system I would not notice, but if I would upload this to my website, it would mysteriously go wrong.
After this I stopped reading this chapter. I liked the security chapter though!

Regarding the constructor, you can’t return anything there, since the constructor will always return the newly created object, regardless of what you say. This is indeed an error in the book.

Regarding the return $this, that’s used to create a so called “fluid interface” where you can “chain” methods. i.e.,


<?php
$parcel = new Parcel();
$parcel->setCountry('Netherlands')->setWeight(10);
?>

as opposed to


<?php
$parcel = new Parcel();
$parcel->setCountry('Netherlands');
$parcel->setWeight(10);
?>

which is what you would do if the methods didn’t return $this.

This chaining is not used very much in PHP, but relied on heavily in javascript libraries like jQuery.

Regarding the autoloading thing, sadly that is indeed completely wrong as well.

I’m fine with your reply. Cheers.