简体   繁体   English

在CodeIgniter中的不同文件之间重用函数的正确方法是什么?

[英]What is the proper way of reusing functions across different files within CodeIgniter?

I'm new to CodeIgniter and I have this function which I use in more than one model. 我是CodeIgniter的新手,并且具有在多个模型中使用的此功能。

// Returns TRUE if the array of values supplied
// each correspond to a column in the database table.
// Else returns the value of the bad column.
private function _columns_exist( $columns, $table ) {

    foreach( $columns as $key => $value ) {

        if( !$this->db->field_exists( $key, $table ) ) {

            return $key;

        }

    }

    return TRUE;

}

What is considered the correct way to write the function once, and reuse it from many different files? 一次编写该函数并从许多不同的文件重新使用该函数的正确方法是什么?

Do I want to create a whole library just for this one function? 我是否要为此功能创建一个整个库?

If you are using a common function across models, you can extend the core model in the first place and get all your other models to extend that. 如果您在模型之间使用通用功能,则可以首先扩展核心模型,并获得所有其他模型来进行扩展。 Read more on extending core classes here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html 在此处阅读有关扩展核心类的更多信息: http : //ellislab.com/codeigniter/user-guide/general/core_classes.html

class MY_Model extends CI_Model {

  public function __construct()
  {
      parent::__construct();
  }

  public function common_func()
  {
    // content
  }
}

then you can use this function in all other models 那么您可以在所有其他型号中使用此功能

class Some_model extends My_Model {

  public function __construct()
  {
      parent::__construct();
  }

  public function some_func()
  {
    // use the common function here
    $result = $this->common_func();
  }
}

The point of CodeIgniter is that there are basically two types of custom functions. CodeIgniter的要点是,基本上有两种类型的自定义函数。 Ones that live in Libraries as library methods and ones that live in Helpers as simple functions. 作为库方法存在于库中的方法,作为简单函数而存在于助手中的方法。

The difference between them is minimal and it is for you to decide where to keep them, but the best practice is that helper functions are mostly standalone and library methods are the ones that use CI libraries like database, sessions, your other models. 它们之间的差异很小,它是由您决定保留它们的位置,但是最佳实践是帮助程序函数大多数是独立的,而库方法则是使用CI库的方法,例如数据库,会话,其他模型。

For your function it's best to make a library and keep it there, but you can, as well, make a helper as you're not overusing any database querying. 对于您的功能,最好创建一个库并将其保留在其中,但是您也可以作为一个助手,因为您不会过度使用任何数据库查询。 If it was me, I would make a helper: 如果是我,我会帮忙:

Make a file in the helpers directory named db_helper.php and add an element db to helper array in autoload.php file. helpers目录中db_helper.php一个名为db_helper.php的文件,并将元素db添加到autoload.php文件中的helper数组中。 Note that you don't need to write the full name db_helper , only db . 请注意,您无需写全名db_helper ,只需写db

Put this code in db_helper.php file you've created: 将此代码放在您创建的db_helper.php文件中:

<?php
if(!function_exists('_columns_exist')) {
    function _columns_exist( $columns, $table ) {
        $CI =& get_instance();
        foreach( $columns as $key => $value ) {
            if( !$CI->db->field_exists( $key, $table ) ) {
                return $key;
            }
        }
        return TRUE;
    }
}

Now you can use this function anywhere - in your controllers, models, even view files as you would if it was a simple function: 现在,您可以在任何地方使用此功能-在您的控制器,模型甚至查看文件中,就像使用简单功能一样:

_columns_exist($columns, $table)

Note that you don't use $this in helpers or libraries, you must get an instance of CodeIgniter singleton object first, then you manipulate it. 请注意,您不在辅助程序或库中使用$this ,必须首先获取CodeIgniter单例对象的实例,然后对其进行操作。 You can find more info on this in the user guide or on StackOverflow. 您可以在用户指南或StackOverflow中找到有关此信息的更多信息。

Also see my comments to darkdad 's answer, that is another great approach I would go for. 另请参阅我对darkdad答案的评论,这是我追求的另一种很好的方法。

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

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