简体   繁体   English

CodeIgniter控制器中的用户功能

[英]user functions in CodeIgniter controller

I need to use some functions in CI controller. 我需要在CI控制器中使用某些功能。

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: 只要foo1和foo2在同一控制器中,您就可以这样做:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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