什么是php中的函数重载和重载? [英] What is function overloading and overriding in php?

查看:67
本文介绍了什么是php中的函数重载和重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,函数重载和函数覆盖是什么意思.两者之间有什么区别?无法弄清楚它们之间有什么区别.

In PHP, what do you mean by function overloading and function overriding. and what is the difference between both of them? couldn't figure out what is the difference between them.

推荐答案

重载正在定义具有相似签名但具有不同参数的函数. 覆盖仅与派生类相关,父类已定义了一个方法,派生类希望覆盖该方法.

Overloading is defining functions that have similar signatures, yet have different parameters. Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that method.

在PHP中,您只能使用魔术方法 __call .

In PHP, you can only overload methods using the magic method __call.

覆盖的示例:

<?php

class Foo {
   function myFoo() {
      return "Foo";
   }
}

class Bar extends Foo {
   function myFoo() {
      return "Bar";
   }
}

$foo = new Foo;
$bar = new Bar;
echo($foo->myFoo()); //"Foo"
echo($bar->myFoo()); //"Bar"
?>

这篇关于什么是php中的函数重载和重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆