在PHP中使用多个参数的同名命名函数 [英] Same named function with multiple arguments in PHP

查看:112
本文介绍了在PHP中使用多个参数的同名命名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Java进行OOP,现在我对PHP很感兴趣。是否有可能用Java中的不同参数创建多个函数?或者,这种语言的解释/非类型性质会阻止这种情况并导致冲突?解析方案

其他人都有很好的代码解释。以下是更高级别的解释:Java支持方法重载,这就是您所指的当你用相同的名称但不同的参数讨论函数时。由于PHP是一种动态类型的语言,这是不可能的。相反,PHP支持默认参数,您可以使用它获得相同的效果。


I started off OOP with Java, and now I'm getting pretty heavy into PHP. Is it possible to create multiples of a function with different arguments like in Java? Or will the interpreted / untyped nature of the language prevent this and cause conflicts?

解决方案

Everyone else has answers with good code explanations. Here is an explanation in more high level terms: Java supports Method overloading which is what you are referring to when you talk about function with the same name but different arguments. Since PHP is a dynamically typed language, this is not possible. Instead PHP supports Default arguments which you can use to get much the same effect.

这篇关于在PHP中使用多个参数的同名命名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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