Welcome Guest, Not a member yet? Register   Sign In
functions inside controllers
#1

[eluser]igorse[/eluser]
I feel a little dumb... anyway, this works:
Code:
public function foo()
{
echo "foo";

function doSomething()
{
  echo "bar";
}

doSomething();
}

but not this
Code:
public function foo()
{
echo "foo";

doSomething();

function doSomething()
{
  echo "bar";
}
}

am I missing something? thanks!
#2

[eluser]LuckyFella73[/eluser]
In your second example the function is declared after your call to it.
You have to declare it before you can call it.
#3

[eluser]igorse[/eluser]
Is this a CI behaviour? I think plain PHP does not have this issue.
#4

[eluser]LuckyFella73[/eluser]
It's not related to CI as far as I know. When you call a function
declared later having everything on "one level" it works but not
inside a function.

Try this as "normal" php file on your server - it wont'work:
Code:
<?php
foo();

function foo()
{
echo "foo";

doSomething();

function doSomething()
{
  echo "bar";
}

}

?>
"foo" is echoed but when the doSomething function is called it's
not seen.

All on "one level" works:
Code:
<?php
echo "foo";

doSomething();

function doSomething()
{
echo "bar";
}
?>
#5

[eluser]CI_expert_indian[/eluser]
Smile
#6

[eluser]igorse[/eluser]
Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB