为什么PHP5中没有超载? [英] Why no overloading in PHP5?

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

问题描述

要完善我的为什么关于PHP的三部曲...... :)如果之前讨论过这个主题,那么我会很感激指向它的指针。我再次找不到它来搜索PHP群组。


PHP手册提到重载

http://no.php。 net / manual / en / language ... erloading.php),但它根本不是真正超载的b $ b ...在某种意义上它并没有被用于其他语言

支持重载(如C ++和Java)。作为该页面上用户提供的用户提供的笔记之一,可以更合适的是将其称为动态方法和属性。什么是超载在PHP中是

左右,是能够做$ object->< name>(...),它会调用

内置函数__call()与< name>和一系列参数。


因为重载在PHP中使用这种相当混乱的意义(与

其他语言相比),它可能有助于简要概述它在C ++和Java中的工作方式。在这些语言中,重载意味着你可能有几个(成员或非成员)具有相同名称的函数,只要

他们的签名是不同的(数字和参数类型)。转换为

PHP,这可能如下所示:


函数f($ a){...} //#1

函数f($ a,$ b){...} //#2

函数f($ a,b $,$ c){...} //#3

函数f(Person $ a){...} //#4


f(1); //调用#1

f(1,test); //电话#2

f(1,2,3); //电话#3

$ obj =新人();

f($ obj); //呼叫#4


最后一次呼叫在技术上也匹配#1,但是类型提示

(人)可能会被认为是更多专业的。


这个问题就像内置类型的类型提示一样,在Zend Q&

A中被提出,比如这个: http://www.zend.com /expert_qa/qas.php?id=10&single=1


---开始报价---


public Object child(){

返回this.child;

}

public Object child(Object p_child){

this.child = p_child;

返回this.child();

}


所以这就是你所说的功能重载?问题: - 为什么称为

函数重载? - 为什么不支持PHP? (重要)


它被称为函数重载,因为你有两个

相同函数名的实例,但它们只有函数参数不同。你

期望根据参数调用正确的一个。它不会被PHP支持,因为它不适合它的动态类型值

范例和执行方法。但是,您可以通过使用可选的函数参数达到类似的影响

,例如:

public Object child(Object p_child = NULL){

if(p_child!= NULL){

this.child = p_child;

}

返回this.child;

}


---结束语---


再一次,我找不到满意的答案,但也许有人在这里可以说服我吗?


即使PHP有松散/弱的打字,那么对于内置的类型提示

类型,值或变量在任何时候都具有特定类型。至少在
之外,可以为参与

用户定义类型(对象)的函数提供重载,就像提供可选静态

以类型提示的形式输入。即:


功能打印(人物$ p){...}

功能打印(物品$ s){...}


评论?


问候,


Terje

To round off my trilogy of "why"''s about PHP... :) If this subject have been
discussed before, I''d appreciate a pointer to it. I again haven''t found it
in a search of the PHP groups.

The PHP manual mentions "overloading"
(http://no.php.net/manual/en/language...erloading.php), but it isn''t
really overloading at all... Not in the sense it''s used in other languages
supporting overloading (such as C++ and Java). As one of the
user-contributed notes on that page says, it would be more appropriate to
call it "dynamic methods and properties". What "overloading" in PHP is
about, is to be able to do $object-><name>(...), and it will call the
built-in function __call() with <name> and an array of the parameters.

Since "overloading" is used in this rather confusing sense (compared to
other languages) in PHP, it may be useful with a brief recap of how it works
in C++ and Java. In these languages, overloading means that you may have
several (member or non-member) functions with the same name, as long as
their signature is different (number and type of arguments). Translated to
PHP, this could look like this:

function f($a) {...} // #1
function f($a, $b) {...} // #2
function f($a, b$, $c) {...} // #3
function f(Person $a) {...} // # 4

f(1); // Calls #1
f(1,"test"); // Calls #2
f(1,2,3); // Calls #3
$obj=new Person();
f($obj); // Calls #4

The last call would technically also match #1, but the one with type hint
(Person) might be considered "more specialised".

This issue has, like type hints for built-in types, been asked in a Zend Q &
A, such as this one: http://www.zend.com/expert_qa/qas.php?id=10&single=1

--- Start quote ---

public Object child() {
return this.child;
}
public Object child(Object p_child) {
this.child = p_child;
return this.child();
}

So this is what you call function overloading? Questions: - Why is it called
function overloading? - Why won''t it be supported in PHP? (important)

It is called function overloading because you have two instances of the
same function name but they differ only by the function arguments. You
expect the right one to be called according to the arguments. It won''t be
supported by PHP because it doesn''t fit in with its dynamically typed value
paradigm and execution methodology. However, you may reach similar affects
by using optional function arguments for example:

public Object child(Object p_child=NULL) {
if (p_child != NULL) {
this.child = p_child;
}
return this.child;
}

--- End quote ---

Again, I don''t find the answer satisfactory, but perhaps someone here can
convince me?

Even if PHP has loose/weak typing, then as for type hints for built-in
types, a value or variable has a specific type at any one time. At the very
least, one might provide overloading for functions taking arguments of
user-defined types (objects), in the same way as one provide optional static
typing in the form of type hints. I.e.:

function print(Person $p) {...}
function print(Something $s) {...}

Comments?

Regards,

Terje

推荐答案

object->< name>(...),它将使用< name>调用

内置函数__call()和一系列参数。


因为重载在PHP中使用这种相当混乱的意义(与

其他语言相比),它可能有助于简要概述它在C ++和Java中的工作方式。在这些语言中,重载意味着你可能有几个(成员或非成员)具有相同名称的函数,只要

他们的签名是不同的(数字和参数类型)。转换为

PHP,这可能如下所示:


函数f(
object-><name>(...), and it will call the
built-in function __call() with <name> and an array of the parameters.

Since "overloading" is used in this rather confusing sense (compared to
other languages) in PHP, it may be useful with a brief recap of how it works
in C++ and Java. In these languages, overloading means that you may have
several (member or non-member) functions with the same name, as long as
their signature is different (number and type of arguments). Translated to
PHP, this could look like this:

function f(


a){... } //#1

函数f(
a) {...} // #1
function f(


a,


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

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