search
Homephp教程php手册Method overloading (rewriting)/method coverage, magic function implementation


Method overloading
The function names are different. The number or parameter types of the function are different, so that the same function name can be called, but different functions can be distinguished
class A{
public function test1(){
echo "test1" ;}
public function test1($a){
echo "test1 hhh";}
}


Overload
$a=newA();
$a->test1();
$a->test1(222);

The above usage is incorrect

Magic function method overloading implementation

class A{
public function test1($p){
echo "accepts one parameter";}
public function test1($p){
echo "accepts two parameters";}
}
provides a __call
__call It is an object that calls a method, and the method does not exist, the system will automatically call __call
function __call($method,$p){

var_dump($p);
if($method=="test1"){
if(count($p)==1){
$this->test1($p);
}else if(count ($p)==2){
$this->test2($p);
}
}
}

$a=newA();
$a->test(1);
$a->test(1,2);


Magic functions
__set,__get,__construct,__destruct,__call,__isset,__unset

__LINE__ how many lines to output
, __FILE__ to output file name
, __DIR__,
__CLASS__ to output class name


Method rewriting/method overwriting (overload)

class Animal{
public $name;
protected $price;
function cry(){
echo "Don't know";}

}
class Dog extends Animal{
//override
function cry(){
echo "puppy";}
}
class Pig extends Animal{
//override
function cry(){
echo "pig" ;}
}

$dog1=new Dog();
$dog1->cry();
$pig=1new Pig();
$pig1->cry();
?>

About rewriting:
When a parent class knows that all subclasses have a method but the parent class is not sure how to write the method, it can let the subclass override this method
1. To implement rewriting, the subclass is required The name of that method is exactly the same as the parameter list, but the parameter names are not required to be the same
2. If the subclass requires calling a method of the parent class (public/protected), you can use parent:: method name (parameters...), Parent class name:: Method name (parameters...)
3. When implementing method coverage, the access modifiers can be different, but they must satisfy the access scope of the subclass >= the access scope of the parent class


Where is polymorphism reflected?
When the subclass does not override the method of the parent class, $call->cry() calls the parent class. If the subclass overrides the method of the parent class, it calls its own cry( )

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft