Yii框架模型中的方法重载 [英] Method overloading in a model of Yii framework

查看:30
本文介绍了Yii框架模型中的方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Yii 框架的模型中重载两个方法.

I want to overload two methods in a model of Yii framework.

在这里,我想说,就像在 Java 中一样,我们有方法重载的概念,这意味着方法具有相同的名称,但传递给该方法的参数数量不同.所以这些类似的概念可以应用在 PHP Yii 框架的模型类中.在 Yii 模型类中,我想创建两个方法,如

Here, I want to say that like in Java we have method overloading concept means method has same name but differs in number of parameters pass to that method. So these similar concept can apply in PHP Yii framework's model class. In Yii model class, I want to create two methods like,

public function test(){ 
   //method logic
}
public function test(int parameters){
  //method logic
}

是否可以像这样创建?我收到无法定义相同的函数"之类的错误.

Is it possible to create like these? I am getting error like "cannot define same function".

推荐答案

PHP 不支持您所说的内容.您可以尝试在 函数参数 中使用默认值.例如.

What you are talking about is not supported in PHP. You could try using default values in function arguments. For eg.

function foo($int_param = 0) // do something

您也可以使用 func_get_args.您将函数定义为不接受参数,然后使用 func_get_args() 获取参数.

You could also use func_get_args. You define your function to not accept parameters and then get the arguments using func_get_args().

如果您尝试实现的功能之间没有太大区别,那么这些方法是合适的.

These approaches are suitable if there is not a lot of difference between the functions that you're trying to implement.

这篇关于Yii框架模型中的方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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