Method overloading in php example

Method Overloading is a concept of Object Oriented Programming which helps in building the composite application in an easy way. Function overloading or method overloading is a feature that permits making creating several methods with a similar name that works differently from one another in the type of the input parameters it accepts as arguments.

The above concept is fine for other programming languages and it is called static polymorphic i.e method overloading.

Example

Let's understand through an example.

Output:

Error

Explanation:

This will generate an error since php will say you have declared this method twice.
But Other programming languages says , doTask[$var1] and doTask[$var1,$var2] are overloaded methods. To call the latter, two parameters must be passed, whereas the former requires only one parameter.
so this behavior i.e decision to call a function at coding time is known as static polymorphic i.e method overloading.

Let's discuss how to achieve method overloading related to PHP5.In the case of PHP, we have to utilize PHP's magic methods __call[] to achieve method overloading.

In PHP overloading means the behavior of method changes dynamically according to the input parameter. In this tutorial, we will understand those perceptions. Let's discuss the __call[] method.

__call[]:

If a class execute __call[], then if an object of that class is called with a method that doesn't exist then__call[] is called instead of that method.

Example

Let's understand method overloading with an example.

Output:

9.426
48

Explanation:

Here area[] method is created dynmically and executed with the help of magic method __call[] and it's behaviour changes according to pass of parametrs as object.

Updated on 31-Dec-2019 08:29:38

  • Related Questions & Answers
  • What is method overloading in C#?
  • What is method overloading in Java?
  • PHP Overloading
  • What is runtime polymorphism or dynamic method overloading?
  • What is the difference between method overloading and method hiding in Java?
  • What is the difference between method overloading and method overriding in Java?
  • What is overloading in C#?
  • What is Overloading in Java?
  • Method overloading in Java
  • What is constructor overloading in Java?
  • What is function overloading in JavaScript?
  • What is overloading? What happens if we overload a main method in java?
  • Function Overloading and Overriding in PHP
  • Using Method Overloading in Java
  • What is overloading a unary operator in C++?

Overloading in PHP provides means to dynamically create properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.

The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms inaccessible properties and inaccessible methods to refer to this combination of declaration and visibility.

All overloading methods must be defined as public.

Note:

None of the arguments of these magic methods can be passed by reference.

Note:

PHP's interpretation of overloading is different than most object-oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.

Property overloading

public __set[string $name, mixed $value]: void

public __get[string $name]: mixed

public __isset[string $name]: bool

public __unset[string $name]: void

__set[] is run when writing data to inaccessible [protected or private] or non-existing properties.

__get[] is utilized for reading data from inaccessible [protected or private] or non-existing properties.

__isset[] is triggered by calling isset[] or empty[] on inaccessible [protected or private] or non-existing properties.

__unset[] is invoked when unset[] is used on inaccessible [protected or private] or non-existing properties.

The $name argument is the name of the property being interacted with. The __set[] method's $value argument specifies the value the $name'ed property should be set to.

Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. A warning is issued if one of the magic overloading methods is declared static.

Note:

The return value of __set[] is ignored because of the way PHP processes the assignment operator. Similarly, __get[] is never called when chaining assignments together like this:

 $a = $obj->b = 8; 

Note:

PHP will not call an overloaded method from within the same overloaded method. That means, for example, writing return $this->foo inside of __get[] will return null and raise an E_WARNING if there is no foo property defined, rather than calling __get[] a second time. However, overload methods may invoke other overload methods implicitly [such as __set[] triggering __get[]].

Example #1 Overloading properties via the __get[], __set[], __isset[] and __unset[] methods

Chủ Đề