11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 6 • OBJECT-ORIENTED <strong>PHP</strong>}}print "Number copies available: {$this->copies}";?>$book = new book("0615303889");This results in the following:Title: Easy <strong>PHP</strong> Websites with the Zend FrameworkNumber copies available: 5Of course, a real-life implementation would likely involve somewhat more intelligent get methods(e.g., methods that query a database), but the point is made. Instantiating the book object results in theautomatic invocation of the constructor, which in turn calls the setIsbn(), getTitle(), andgetNumberCopies() methods. If you know that such methods should be called whenever a new object isinstantiated, you’re far better off automating the calls via the constructor than attempting to manuallycall them yourself.Additionally, if you would like to make sure that these methods are called only via the constructor,you should set their scope to private, ensuring that they cannot be directly called by the object or by asubclass.Invoking Parent Constructors<strong>PHP</strong> does not automatically call the parent constructor; you must call it explicitly using the parentkeyword. An example follows:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!