Change language

Overloading in PHP

Function overloading in PHP?
Function overloading in PHP is used to dynamically create properties and methods. These dynamic objects are processed by magic methods that can be used in the class for various types of actions. Function overloading contains the same function name, and this function performs different tasks depending on the number of arguments. For example, find an area of ​​certain shapes where the radius is given, then it should return the area of ​​the circle, if the height and width are specified, then it should give the area of ​​the rectangle-method/">rectangle and others. Like other OOP languages, function overloading cannot be done natively. In PHP, function overloading is done using the __call() magic function. This function takes a function name and arguments.Property and overloading rules in PHP:
  • All overloading methods must be defined as Public.
  • After creating an object for a class, we can access a collection of entities that are properties or methods not defined in the scope of the class.
  • Such objects are called overloaded properties or methods, and the process is called overloading .
  • PHP magic methods are used to manipulate these overloaded properties or functions.
  • Most magic methods will run in the context of an object, except for the __callStatic() method, which is used in a static context.
Types of overloading in PHP: there are two types of overloading in PHP .
  • Property overloading
  • Method overloading
Property Overloading: PHP Property Overloadingis used to create dynamic properties in the context of of the object. You don’t need a separate line of code to create these properties. A property associated with an instance of a class, and if not declared within the class, it is considered an overloaded property. 
The following operations are performed on overloaded properties in PHP.
  • Setting and getting overloaded properties.
  • Evaluating overloaded property settings.
  • Cancel the setting such properties.
Before performing operations, we must define the appropriate magic methods. which,
  • __set():fires when overloaded properties are initialized.
  • __get():fires when used overloaded properties with PHP print statements.
  • __isset():this magic method is called when we check overloaded properties with isset()
  • __ unset():Likewise, this function will be called when PHP unset() is used for overloaded properties.
Example :  
// class GFG { // Data overload location private $data = array (); // No overloading is used here public $declared = 1; // Overload is used on access // outside the class private $hidden = 2; // Function definition public function __ set ( $name , $value ) { echo " Setting ’$name’ to’ $value’ " ; $this -> data [ $name ] = $value ; } // Function definition public function __ get ( $name ) { echo "Getting’ $name: " ; if ( array_key_exists ( $name , $this -> data)) { return $this -> data [ $name ]; } $trace = debug_backtrace(); return null; } // Function definition public function __ isset ( $name ) { echo "Is’ $name’ set? " ; return isset ( $this -> data [ $name ]); } // __unset function definition public function __ unset ( $name ) {   echo "Unsetting’ $name’ " ; unset ( $this -> data [ $name ]); } // getHidden function definition public function getHidden ( ) { return $this -> hidden; } }  
// Create object $obj = new GFG;  
// Set object variable to 1 $obj -> a = 1; echo $obj -> a ... "" ;  
// Use the isset function to check // "a" is set or not var_dump (isset ( $obj -> a));  
// Reset ’ a ’ unset ( $obj -> a); var_dump (isset ( $obj -> a)); echo $obj -> declared. "" ; echo " Private property are visible inside the class " ; echo $obj -> getHidden(). "" ; echo " Private property are not visible outside of class " ; echo $obj -> hidden ... "" ;  
?>
Exit:
 Setting ’a ’to’ 1’ Getting ’a: 1 Is’ a’ set? bool (true) Unsetting ’a’ Is’ a’ set? bool (false) 1 Private property are visible inside the class 2 Private property are not visible outside of class Getting ’hidden: 
Method overloading:is an overload type for creating dynamic methods, which are not declared in class scope. PHP method overloading also runs magic methods designed for a specific purpose. Unlike property overloading, PHP method overloading allows you to call functions on both an object and a static context. 
Related magic functions,
  • __call() & # 8212; fires when overloaded methods are called in the context of an object.
  • __callStatic() & # 8212; fires when overloaded methods are called in a static context.
Example : class GFG { public function __ call ( $name , $arguments ) { echo "Calling object method’ $name’ " . implode ( ’,’ , $arguments ). "" ; } public static function __ callStatic ( $name , $arguments ) { echo " Calling static method ’$name’" . implode ( ’,’ , $arguments ). "" ; } }  
// Create a new object $obj = new GFG; $obj -> runTest ( ’ in object context’ ); GFG::runTest ( ’in static context’ );  
?>
Exit:
 Calling object method ’runTest’ in object context Calling static method’ runTest’ in static context 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method