This topic is part of our Object-Oriented Programming in PHP training
When version 5.0 was released in 2004, PHP became a full-fledged Object-Oriented Programming Language (similar to Java and C#).
Exercises
We will implement a web application which manages people, dogs and cars in a number of steps until we have explored the most important object-oriented constructs found in PHP5:
- Implement the Person and Dog classes (+ required constructors, methods, attributes) and instantiate one object from each class.
- Add visibility constructs (private for attributes / public for methods).
- Add the Car class with its association as shown in the above diagram. Given that PHP is dynamically typed, what is the risk here?
- Use type hints to prevent type mismatch.
- Implement the Nameable interface as well as the Nameables class (use an array to hold references). Implement getNames() using foreach(). Use type hints to prevent type mismatch.
- Use an exceptions for error handling when someone attempts to create a Car with no number.
- Replace the exception by a custom exception.
- Use inheritance (sparingly) to create a specialized version of Person called Doctor with a special behaviour for getName().
- Replace the array in Nameables by a SplDoublyLinkedList of the Standard PHP library.
- Use mktime to make sure that each Car created has a registration date of 25 December 2010 at 10:00
- Use strtotime to make sure that each Car created has a first servicing date which is next Saturday at 10:00.
- Use strtotime to make sure that each Car created has a second servicing date which is exactly three months after the first one.
- Use a static attribute which are shared among all Cars to keep a count of all Cars which have been instantiated.
This topic is part of our Object-Oriented Programming in PHP training
Leave a Reply