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.
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