For this PHP code exercise we will explore Object Oriented Programming in PHP by creating Shape classes: Shape.php, Rectangle.php, and Circle.php.
Shape.php is the base class. Shape class requirements are:
- A class constant named
SHAPE_TYPEwith a value of1. - Four properties with different visibilities: a public
name, a protectedlengthandwidth, and a privateid. - A constuctor which accepts a
lengthandwidthparameter to initialize the respective properties. - The constructor shoudl also initialize the
idproperty to a unique id. (hint: use PHP'suniqid()) - Getter and Setter methods for the
nameproperty. - A Getter method for the
idproperty. - A public
areamethod which calculates and returns the area of theShapeobject. - A public static
getTypeDescriptionmethod which returns the stringType:with theSHAPE_TYPEconcatenated to the end. - A public
getFullDescriptionmethod which returns the string:Shape<#id>: name - length x widthwith variablesid,name,length, andwidthsubstituted the objects values.
Rectangle.php should inherit from the Shape class. Rectangle class requirements are:
- A class constant named
SHAPE_TYPEwith a value of2.
Note: if you used inheritance properly, the Rectangle class should not require any properties or methods.
Circle.php should inherit from the Shape class. Circle class requirements are:
- A class constant named
SHAPE_TYPEwith a value of3. - A protected
radiusproperty. - A constuctor which accepts a
radiusparameter and initializes theradiusproperty. **Don't forget to call theShapeclass constructor. - A public
areamethod which calculates and returns the area of theCircleobject. - A public
getFullDescriptionmethod which returns the string:Circle<#id>: name - radiuswith variablesid,name, andradiussubstituted the objects values.
Review the PHP Code Exercises documentation for more details on performing code exercises.
Jump on the PHP channel in Slack and ask your fellow students and mentors for a hint.