Method overload (override)/method override, magic function implementation, and heavy-load magic

Source: Internet
Author: User
Tags types of functions

Method overload (override)/method override, magic function implementation, and heavy-load magic


Method overload
Different function names use different numbers or types of functions to call the same function name, but different functions can be distinguished.
Class {
Public function test1 (){
Echo "test1 ";}
Public function test1 ($ ){
Echo "test1 hhh ";}
}


Heavy Load
$ A = newA ();
$ A-> test1 ();
$ A-& gt; test1 (222 );

The above usage is incorrect.

Magic Function Method overload implementation

Class {
Public function test1 ($ p ){
Echo "accepting a parameter ";}
Public function test1 ($ p ){
Echo "two parameters are accepted ";}
}
Provides a _ call
_ Call is an object that calls a method. If the method does not exist, the system automatically calls _ 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 _ number of output rows
,__ FILE _ output FILE name
,__ DIR __,
_ CLASS _ name of the output CLASS


Method override/overload)

<? Php
Class Animal {
Public $ name;
Protected $ price;
Function cry (){
Echo "unknown ";}

}
Class Dog extends Animal {
// Overwrite
Function cry (){
Echo "puppy ";}
}
Class Pig extends Animal {
// Overwrite
Function cry (){
Echo "Piggy ";}
}

$ Dog1 = new Dog ();
$ Dog1-> cry ();
$ Pig = 1new Pig ();
$ Pig1-> cry ();
?>

About Rewriting:
When a parent class knows that all child classes have a method, but the parent class cannot determine how to write the method, the Child class can overwrite the method.
1. to rewrite the method, the method of the subclass must have the same name as the parameter list, but the parameter name is not required to be the same.
2. if the subclass requires that a method (public/protected) of the parent class be called, you can use parent: Method Name (parameter ...), parent Class Name: Method Name (parameter ...)
3. When the implementation method overwrites, the access modifier can be different, but must satisfy the access range of the subclass> = access range of the parent class


What is polymorphism?
If the subclass does not overwrite the parent class method, $ call-> cry () calls the parent class. If the subclass overwrites the parent class method, it calls its own cry ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.