简体   繁体   中英

user functions in CodeIgniter controller

I need to use some functions in CI controller.

For example:

class Main extends Controller
{
    function index()
    {
        function foo1(){}
        function foo2(){}
    }
}

But I get an error. How to determine these functions?

As long as foo1 and foo2 are in the same controller you can do this:

class Main extends Controller
{
    function index()
    {
        $this->foo1();
        $this->foo2();
    }

    public function foo1()
    {
    }

    public function foo2()
    {
    }
}

If you use the function declaration syntax inside another function, the inside function will end up in the current namespace (or the global namepsace if no namespace was declared)

consider this example:

Class Foo {
    public function bar() {
        function foo(){
            print 'in foo';
        }
    }
}

$f = new Foo();
$f->bar(); // you have to call this before invoking the foo() function, prior this point its nonexistent
foo(); // will print 'in foo'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM