PHP Method Overloading

Overloading in PHP

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. 

PHP Method Overloading 

It is a type of overloading for creating dynamic methods that are not declared within the class scope. PHP method overloading also triggers magic methods dedicated to the appropriate purpose. Unlike property overloading, PHP method overloading allows function call on both object and static context.

 The related magic functions are.

__call(string $name, array $arguments) –  is triggered when invoking inaccessible methods in an object context.

__callStatic(string $name, array $arguments) – is triggered when invoking inaccessible methods in a static context.

The $name argument is the name of the method being called. The $arguments argument is an enumerated array containing the parameters passed to the $name'ed method.


<?php
class Wali
{
    public function 
__call($name$arguments)
    {
        
// Note: value of $name is case sensitive.
        
echo "Calling object method '$name' "
             
implode(', '$arguments). "\n";
    }

    public static function 
__callStatic($name$arguments)
    {
        
// Note: value of $name is case sensitive.
        
echo "Calling static method '$name' "
             
implode(', '$arguments). "\n";
    }
}

$obj = new Wali;
$obj->runTest('in object context');

Wali::runTest('in static context');
?>


Output:

Calling object method 'runTest' in object context

Calling static method 'runTest' in static context


3 comments:

Dynamic Vishva said...

This is extremely pleasant data, Thank you such a great amount for sharing your! information. Continue to share

Mobile Application Development Companies in Mumbai
Custom Android App
Custom Android App
E commerce mobile apps

Betty J. Marroquin said...

Such a nice post!
Useful Links =>
Software Development Company
Software Development Services
Graphic Design and Video Editing Services
Website Design and Development Services
Mobile Application Development Services
Digital Marketing Services
Content Writing Services
Latest Software Development Blogs
Software Development Company Blogs
WordPress Premium Theme 2021

Emma said...

Web Development in Dubai
https://www.nsreem.com/ourservices/web-development/
If you are looking for develop customized Dynamic Web Development in Dubai, E-Commerce Web Development in Dubai then NSREEM is #1 Web Development Company in India.
1634960544483-8

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...