我们可以在Codeigniter控制器中创建两个具有相同名称的函数吗?

我们可以在Codeigniter控制器中创建两个具有相同名称的函数吗?

问题描述:

Is there any way I can create 2 functions in Codeigniter controller where one of the function contains argument and the other doesn't. I am using the below code which is giving me an error.

class example extends CI_Controller {
    function show() {
        #code goes here
    }

   function show($id) {
       #code goes here
    }
}

But it's showing me an error that function name cannot be same.

我有什么方法可以在Codeigniter控制器中创建2个函数,其中一个函数包含参数而另一个函数不包含 吨。 我使用下面的代码,它给了我一个错误。 p>

 类示例扩展CI_Controller {
函数show(){
 #code在这里
} 
  
函数show($ id){
 #code在这里
} 
} 
  code>  pre> 
 
 

但是它显示了一个错误,函数名称不能相同 。 p> div>

No, but you can do this:

function show($id = null)
{
    if ($id === null) {
        // $id was not passed
    } else {
        // $id was passed
    }
}

According to the PHP manual not:

PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions. http://php.net/manual/en/functions.user-defined.php

However with Runkit it's possible to do so:

<?php
function testme() {
  echo "Original Testme Implementation
";
}
testme();
runkit_function_redefine('testme','','echo "New Testme Implementation
";');
testme();
?>

http://us3.php.net/manual/en/function.runkit-function-redefine.php