PHP Object-Oriented Solutions
Chapter 2
This chapter gives an overview of writing PHP classes. It discusses using child and superclasses, handling visibility, static methods and variables, and constants.
Exericses
- Using class constraints for properties
- The exercise shows two constrains. As ClassB is a child of ClassA, the constraint has been overwritten.
- Creating static properties and methods
- This exercise shows how static elements can be manipulated. The static variable is set to 0, but when the class is called, the number is increased by one. In the exercise, the first instance is set to 1 and the second is set to 2.
- Experimenting with Type Hinting
- This exercise explains how to use type hinting. In the first instance (not shown), the type hinting was limited to the
Book
class. When the DVD was added, it though a fatal error. In the second instance (shown), the type hinting is expanded to theProduct
class, allowing all its subclasses to be used. - Accessing Methods Automatically
- The method demonstrated will make sure the method exists, then runs it if it proves true.
- Throwing and Catching an Exception
- The page shows what the various methods of showing an exception display when called.