Lokang 

PHP and MySQL

constructor

A constructor in PHP is a special method that is automatically called when an object of a class is created. It is used to initialize the properties of the class. The constructor method in PHP is named __construct.

Example: Using a Constructor

Let's expand on the previous Calculator example by adding a constructor that initializes some properties.

<?php
// Define the Calculator class
class Calculator {
   // Properties
   private $a;
   private $b;
   // Constructor
   public function __construct($a, $b) {
       $this->a = $a;
       $this->b = $b;
   }
   // Method to add the properties
   public function add() {
       return $this->a + $this->b;
   }
   // Method to subtract the properties
   public function subtract() {
       return $this->a - $this->b;
   }
   // Method to multiply the properties
   public function multiply() {
       return $this->a * $this->b;
   }
   // Method to divide the properties
   public function divide() {
       if ($this->b == 0) {
           return "Error: Division by zero";
       }
       return $this->a / $this->b;
   }
}
// Create an instance of the Calculator class
$calculator = new Calculator(10, 5);
// Call the methods of the Calculator class and display the results
echo "Addition: " . $calculator->add() . "\n";         // Outputs: Addition: 15
echo "Subtraction: " . $calculator->subtract() . "\n"; // Outputs: Subtraction: 5
echo "Multiplication: " . $calculator->multiply() . "\n"; // Outputs: Multiplication: 50
echo "Division: " . $calculator->divide() . "\n";         // Outputs: Division: 2
?>

Explanation

Class Definition:

  • The class Calculator defines a new class named Calculator.
  • Properties $a and $b are defined as private properties of the class.

Constructor:

  • The __construct($a, $b) method is defined to initialize the properties $a and $b when a new object is created.
  • private $a; and private $b; define the properties that will hold the values.

Methods:

  • add(), subtract(), multiply(), and divide() are methods defined within the class to perform arithmetic operations on the properties $a and $b.

Creating an Instance:

  • $calculator = new Calculator(10, 5); creates an instance of the Calculator class and initializes the properties $a and $b with the values 10 and 5.

Calling Methods:

  • echo $calculator->add(); calls the add method on the $calculator object and prints the result.
  • Similarly, other methods (subtract, multiply, divide) are called and their results are printed.

Constructor with Default Values

You can also define a constructor with default values for the parameters:

<?php
class Calculator {
   // Properties
   private $a;
   private $b;
   // Constructor with default values
   public function __construct($a = 0, $b = 0) {
       $this->a = $a;
       $this->b = $b;
   }
   // Methods
   public function add() {
       return $this->a + $this->b;
   }
   public function subtract() {
       return $this->a - $this->b;
   }
   public function multiply() {
       return $this->a * $this->b;
   }
   public function divide() {
       if ($this->b == 0) {
           return "Error: Division by zero";
       }
       return $this->a / $this->b;
   }
}
// Create instances of the Calculator class
$calculator1 = new Calculator(10, 5);
$calculator2 = new Calculator();
// Call the methods of the Calculator class and display the results
echo "Calculator1 Addition: " . $calculator1->add() . "\n"; // Outputs: Calculator1 Addition: 15
echo "Calculator2 Addition: " . $calculator2->add() . "\n"; // Outputs: Calculator2 Addition: 0
?>

Explanation

Constructor with Default Values:

  • The __construct($a = 0, $b = 0) method is defined to initialize the properties with default values if no arguments are passed when the object is created.

Creating Instances:

  • $calculator1 = new Calculator(10, 5); creates an instance with specific values.
  • $calculator2 = new Calculator(); creates an instance with default values.

Calling Methods:

  • Methods are called on both instances to demonstrate the use of default values in the constructor.

Summary

  • Constructor: A special method named __construct that initializes the properties of the class.
  • Creating an Instance: Use new ClassName(parameters) to create an object and pass arguments to the constructor.
  • Default Values: Constructors can have default values for parameters to handle cases where no arguments are provided.