overloading
same funciton in one class with same parameters is not allwo
same function with two different parameters in one class is allowed
and
overiding
parent and child have same funciton , which one implement ist is which class calls
encapsulation
to call protected function from another calss
polymorphism means
inheticance
a extends b
b extends c
c extends d
Sure! I can provide you with examples of method overloading and method overriding in PHP.
- Method Overloading:
Method overloading refers to the ability to define multiple methods with the same name but different parameters in a class. In PHP, method overloading is not supported directly, but you can achieve similar behavior using various techniques, such as using default parameter values or using the func_get_args()
function. Here's an example:
s
class Calculator { public function add($num1, $num2) { return $num1 + $num2; }
public function add($num1, $num2) { return $num1 + $num2; } public function addThreeNumbers($num1, $num2, $num3 = 0) { return $num1 + $num2 + $num3; } public function addMultipleNumbers() { $args = func_get_args(); $sum = 0; foreach ($args as $num) { $sum += $num; } return $sum; } } $calc = new Calculator(); echo $calc->add(2, 3); // Outputs: 5 echo $calc->addThreeNumbers(2, 3, 5); // Outputs: 10 echo $calc->addMultipleNumbers(2, 3, 5, 7); // Outputs: 17
add repeated two times -> overloading-error
In the example above, the Calculator
class demonstrates different approaches to achieve method overloading. The add()
method adds two numbers, while the addThreeNumbers()
method adds three numbers, with the third number having a default value of 0. The addMultipleNumbers()
method utilizes the func_get_args()
function to add an arbitrary number of arguments.
- Method Overriding:
Method overriding refers to the ability of a subclass to provide its implementation for a method that is already defined in its parent class. In PHP, method overriding is achieved by redefining a method in the subclass with the same name and parameters as in the parent class. Here's an example:
s
class Shape {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function getArea() {
return 0;
}
}
class Circle extends Shape {
private $radius;
public function __construct($name, $radius) {
parent::__construct($name);
$this->radius = $radius;
}
public function getArea() {
return pi() * pow($this->radius, 2);
}
}
$circle = new Circle("Circle", 5);
echo $circle->getArea(); // Outputs: 78.539816339745
s
In the example above, the Shape
class has a getArea()
method that returns 0. The Circle
class extends Shape
and overrides the getArea()
method with its own implementation to calculate the area of a circle using the provided radius.
When you call $circle->getArea()
, the overridden method in the Circle
class is invoked, returning the area of the circle based on the radius value provided.
These examples illustrate how you can achieve method overloading and method overriding in PHP, even though PHP doesn't have native support for method overloading.
No comments:
Post a Comment