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_TYPE
with a value of1
. - Four properties with different visibilities: a public
name
, a protectedlength
andwidth
, and a privateid
. - A constuctor which accepts a
length
andwidth
parameter to initialize the respective properties. - The constructor shoudl also initialize the
id
property to a unique id. (hint: use PHP'suniqid()
) - Getter and Setter methods for the
name
property. - A Getter method for the
id
property. - A public
area
method which calculates and returns the area of theShape
object. - A public static
getTypeDescription
method which returns the stringType:
with theSHAPE_TYPE
concatenated to the end. - A public
getFullDescription
method which returns the string:Shape<#id>: name - length x width
with variablesid
,name
,length
, andwidth
substituted the objects values.
Rectangle.php
should inherit from the Shape
class. Rectangle
class requirements are:
- A class constant named
SHAPE_TYPE
with 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_TYPE
with a value of3
. - A protected
radius
property. - A constuctor which accepts a
radius
parameter and initializes theradius
property. **Don't forget to call theShape
class constructor. - A public
area
method which calculates and returns the area of theCircle
object. - A public
getFullDescription
method which returns the string:Circle<#id>: name - radius
with variablesid
,name
, andradius
substituted 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.