Tools for Building Object Hierarchies
In this section, we delve even deeper into object-oriented design issues. We have already seen how useful the hints are in method arguments introduced with PHP 5. Hints are important because we can be sure that when using them we will be working with an object of a particular type. We encountered this earlier in this hour:
public function update( Item $item ) { print "updating.. "; print $item->name; }
The update() method knows that it has an Item object and can go ahead and work with the $name property that it knows will be accessible. So by constraining the type of the argument passed to the method, we ensure the interface of the object.
Abstract classes and interfaces are ways of doing a similar thing. ...
Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.