Lokang 

PHP and MySQL

Class and Methods

Defining and Using Methods Inside a Class

Let's define a class called Calculator with methods for basic arithmetic operations: addition, subtraction, multiplication, and division. We'll then create an instance of the class and call these methods.

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

Explanation

Class Definition:

  • The class Calculator defines a new class named Calculator.
  • Methods add, subtract, multiply, and divide are defined within the class to perform arithmetic operations.

Methods:

  • add($a, $b): Adds two numbers.
  • subtract($a, $b): Subtracts the second number from the first number.
  • multiply($a, $b): Multiplies two numbers.
  • divide($a, $b): Divides the first number by the second number, with a check to prevent division by zero.

Creating an Instance:

  • $calculator = new Calculator(); creates an instance of the Calculator class.

Calling Methods:

  • echo $calculator->add(10, 5); 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.

Calling Methods with Parameters

When calling a method, you pass arguments to it, which the method uses to perform its operations. In the example above, each arithmetic method takes two parameters ($a and $b).

<?php
// Call the add method
$result = $calculator->add(8, 3);
echo "8 + 3 = $result\n"; // Outputs: 8 + 3 = 11
// Call the subtract method
$result = $calculator->subtract(8, 3);
echo "8 - 3 = $result\n"; // Outputs: 8 - 3 = 5
// Call the multiply method
$result = $calculator->multiply(8, 3);
echo "8 * 3 = $result\n"; // Outputs: 8 * 3 = 24
// Call the divide method
$result = $calculator->divide(8, 3);
echo "8 / 3 = $result\n"; // Outputs: 8 / 3 = 2.66666666667

Summary

Defining a Class:

  • Use class ClassName { ... } to define a class.
  • Define methods inside the class using public function methodName($parameters) { ... }.

Creating an Instance:

  • Use new ClassName() to create an instance of the class.

Calling Methods:

  • Use $object->methodName(arguments) to call methods on the object.

This example demonstrates how to define methods inside a class, create an instance of the class, and call the methods with parameters.