【问题标题】:CodeIgniter: How to load my own class of static functions into a controller?CodeIgniter:如何将我自己的静态函数类加载到控制器中?
【发布时间】:2016-03-29 03:59:39
【问题描述】:

我有一类静态函数(常用实用函数),我希望将其加载到 codeigniter 中。目前我使用include_once(...) 正常加载它,它按预期工作。

但是,我想使用 codeigniter 的方法来加载它。我知道我应该将我的类文件保存到 third_party 目录中;并且我应该创建一个需要我的类的库类(将其保存在库目录中)。

下面是三个组件,但它不起作用。

1

// my class, saved at: APPPATH.'third_party/My_Class.php'
class My_Class  
  {
    public static function my_static_utility_method (  )
      {
        return "booger" ;
      }

    // ...
  }

2 - 我知道我应该创建一个遵循“库”实例化的 CI 规则的包装器:

// saved at: APPPATH.'libraries/Library_Wrapper.php'
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Library_Wrapper 
  {
    public function __construct()
      {
        require_once APPPATH.'third_party/My_Class.php';
      }
  }

3 - 现在我想从我的控制器访问 My_Class 的静态方法:

// saved at: APPPATH.'controllers/my_controller.php'
class My_Controller extends CI_Controller
  { 

    public function __constructor( )
      {
        parent::__construct();
        $this->load->library( 'Library_Wrapper' ) ; 
      }

    public function some_function()
      {
        echo $this->My_Class->my_static_utility_method( ) ;
      }

  }

【问题讨论】:

    标签: codeigniter codeigniter-3


    【解决方案1】:

    尝试以下步骤:

    1. 创建一个新的控制器文件:application/core/MY_Controller.php。在该文件中,您可以添加静态函数。

      <?php
      
      class MY_Controller extends CI_Controller {
      
          protected function static_fn1() {
              //code
          }
      }
      
    2. 需要访问静态函数的控制器可以扩展此类,例如: 文件:application/controllers/Welcome.php

      <?php
      
      class Welcome extends MY_Controller {
      
          public function fnname() {
              //code
          }
      }
      

    【讨论】:

    • 如果使用 CI3 及以上,应应用控制器的类和文件名的首字母如这里所说codeigniter.com/user_guide/general/…
    • 我不相信大写字母实际上有什么真正的功能作用??
    【解决方案2】:

    您几乎已经完成了第三步,但还没有完成。加载库将使用与加载它的名称相同的名称:

    //Load library
    $this->load->library( 'Some_name' );
    //Use Library
    $this->some_name->someFunction();
    

    因此,在您的情况下,您需要切换从以下位置访问库的方法:

    //Will throw an PHP undefined My_Class error
    echo $this->My_Class->my_static_utility_method();
    

    改为Library_wrapper

    //from $this->load->library( 'Library_wrapper' );
    echo $this->library_wrapper->my_static_utility_method();
    

    但这会带来下一个问题,因为 My_Class 是 library_wrapper 的属性,所以调用它会有点冗长:

    echo $this->library_wrapper->My_Class->my_static_utility_method();
    

    如果可公开访问,应该成功调用 My_Class 后代方法。

    这并不像您希望的那样干净。最好将My_Class 扩展为Library_wrapper,而不是共享静态实例:

    /**
     * Static helper methods:
    **/
    class Library_wrapper extends My_Class {
    
    }
    

    可以将“加载”库绑定到 different name(请参阅“将库分配给不同的对象名称”标题)。

    【讨论】:

    • 不幸的是它不工作......我仍然得到Fatal error: Call to a member function my_static_utility_method() on a non-object in C:\Server\htdocs\...\my_controller.php on line 25......但是我也会尝试从图书馆扩展(而不是要求)......谢谢你答案和时间
    • 另一位评论者@wolfgang1983 是正确的,您在 CodeIgnitier 中调用的类和方法的区分大小写确实很重要。尤其是那些从控制器调用的,因为在 PHP 中,类属性区分大小写 (See example),所以当涉及到 $this-&gt;load 方法时,请记住考虑 ucfirst
    【解决方案3】:

    您可以尝试以下解决方案:

    在这个层次结构中 $this->library_wrapper->My_Class->my_static_utility_method();

    使用Php Functions 中列出的 php 函数找出层次结构中每个点的方法/变量。这将精确定位出问题的确切位置。

    【讨论】:

      【解决方案4】:

      我认为你不需要构建一个类。最简单的方法是帮助器(“帮助器,顾名思义,帮助您完成任务。每个帮助器文件只是特定类别中的函数集合” - 只需阅读来自 @ 的文章987654321@)。

      您的名为 myfunctions_helper.php 的帮助文件将位于 /application/helpers 文件夹中,并且可能具有如下结构:

      <?php
      defined('BASEPATH') OR exit('No direct script access allowed');
      
      if (!function_exists('my_static_fuction')) {
         function my_static_fuction (  )
         {
            return "booger" ;
         }
      }
      
      // etc...
      

      然后您可以自动加载将其声明到/application/config/autoload.php 的助手。

      $autoload['helper'] = array('url','myfunctions');
      

      如果您需要构建一个类,那么像@MackieeE 这样的库就可以完成这项工作(check the tutorial 了解更多信息)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多