Creating Constructors
A constructor is a special kind of method that differs from standard ones in three ways:
- Its name is always _ _construct().
- It is automatically and immediately called whenever an object of that class is created.
- It cannot have a return statement.
The syntax for defining a constructor is therefore
class ClassName { public $var; function _ _construct() { // Function code. } }
A constructor could be used to connect to a database, set cookies, or establish initial values. Basically, you’ll use constructors to do whatever should always be done—and done first—when an object of this class is made.
Because the constructor is still just another method, it can take arguments, and values for those arguments can be provided when the object is created:
class User { function _ _construct($id) { // Function code. } } $me = new User(2354);
The Rectangle class could benefit from having a constructor that assigns the rectangle’s dimensions when the rectangle is created.
To add and use a constructor:
- Open Rectangle.php (Script 4.3) in your text editor or IDE.
- After declaring the attributes and before defining the setSize() method, add the constructor (Script 4.5):
function _ _construct($w = 0, $h = 0) { $this->width = $w; $this->height = $h; }
Script 4.5. A constructor has been added to the Rectangle class. This makes it possible to assign the rectangle’s dimensions when the object is created.
1 <?php # Script 4.5 - Rectangle.php 2 /* This page defines the Rectangle class. 3 * The class contains two attributes: width and height. 4 * The class contains five methods: 5 * - _ _construct() 6 * - setSize() 7 * - getArea() 8 * - getPermeter() 9 * - isSquare() 10 */ 11 12 class Rectangle { 13 14 // Declare the attributes: 15 public $width = 0; 16 public $height = 0; 17 18 // Constructor: 19 function _ _construct($w = 0, $h = 0) { 20 $this->width = $w; 21 $this->height = $h; 22 } 23 24 // Method to set the dimensions: 25 function setSize($w = 0, $h = 0) { 26 $this->width = $w; 27 $this->height = $h; 28 } 29 30 // Method to calculate and return the area: 31 function getArea() { 32 return ($this->width * $this->height); 33 } 34 35 // Method to calculate and return the perimeter: 36 function getPerimeter() { 37 return ( ($this->width + $this->height) * 2 ); 38 } 39 40 // Method to determine if the rectange 41 // is also a square. 42 function isSquare() { 43 if ($this->width == $this->height) { 44 return true; // Square 45 } else { 46 return false; // Not a square 47 } 48 49 } 50 51 } // End of Rectangle class.
This method is exactly like the set-Size() method, albeit with a different name. Note that constructors are normally the first method defined in a class (but still defined after the attributes).
- Save the file as Rectangle.php.
- Open rectangle1.php (Script 4.4) in your text editor or IDE.
If you want, change the values of the $width and $height variables (Script 4.6):
$width = 160; $height = 75;
Change the way the object is created so that it reads
$r = new Rectangle($width, $height);
The object can now be created and the rectangle assigned its dimensions in one step.
Delete the invocation of the setSize() method.
This method is still part of the class, though, which makes sense. By keeping it in there, you ensure that a rectangle object’s size can be changed after the object is created.
Script 4.6. This new version of the script assigns the rectangle’s dimensions when the object is created (thanks to the constructor).
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Rectangle</title> 6 <link rel="stylesheet" href="style.css"> 7 </head> 8 <body> 9 <?php # Script 4.6 - rectangle2.php 10 /* This page uses the revised Rectangle class. 11 * This page shows a bunch of information 12 * about a rectangle. 13 */ 14 15 // Include the class definition: 16 require('Rectangle.php'); 17 18 // Define the necessary variables: 19 $width = 160; 20 $height = 75; 21 22 // Print a little introduction: 23 echo "<h2>With a width of $width and a height of $height...</h2>"; 24 25 // Create a new object: 26 $r = new Rectangle($width, $height); 27 28 // Print the area. 29 echo '<p>The area of the rectangle is ' . $r->getArea() . '</p>'; 30 31 // Print the perimeter. 32 echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter() . '</p>'; 33 34 // Is this a square? 35 echo '<p>This rectangle is '; 36 if ($r->isSquare()) { 37 echo 'also'; 38 } else { 39 echo 'not'; 40 } 41 echo ' a square.</p>'; 42 43 // Delete the object: 44 unset($r); 45 46 ?> 47 </body> 48 </html>
- Save the file as rectangle2.php, place it in your Web directory along with the new Rectangle.php (Script 4.5), and test in your Web browser .