【问题标题】:user functions in CodeIgniter controllerCodeIgniter 控制器中的用户函数
【发布时间】:2012-11-13 07:08:52
【问题描述】:

我需要在 CI 控制器中使用一些功能。

例如:

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

但是我得到一个错误。如何确定这些函数?

【问题讨论】:

  • 您遇到了什么错误,您使用的是什么版本的 CI?另外,您的实际代码是什么?
  • 你想确定什么?
  • 您在哪里(具体地)定义了函数?

标签: php codeigniter frameworks


【解决方案1】:

只要 foo1 和 foo2 在同一个控制器中,您就可以这样做:

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

    public function foo1()
    {
    }

    public function foo2()
    {
    }
}

【讨论】:

    【解决方案2】:

    如果你在另一个函数内部使用函数声明语法,内部函数将在当前命名空间中结束(如果没有声明命名空间,则为全局命名空间)

    考虑这个例子:

    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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多