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

Method overloading (rewriting)/method coverage, magic function implementation

WBOY
Release: 2016-10-24 00:00:00
Original
1808 people have browsed it


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( )

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template