代码点火器和配置文件中的自定义库?

发布于 2024-11-09 10:43:22 字数 342 浏览 6 评论 0原文

我在代码点火器中有一个库,看起来像 class MyClass($options = array())

文件是 Myclass.php

我有一个文件 (config/ Myclass.php)看起来像

$Myclass = array(
  'something' => 'value',
  'another'   => 'value'
);

我认为在初始化我的类时应该传递 $Myclass 数组,但显然不是?

我需要做什么来修复它?

I have a library in code igniter that looks like class MyClass($options = array())

The file is Myclass.php

I have a file (config/Myclass.php) that looks like

$Myclass = array(
  'something' => 'value',
  'another'   => 'value'
);

Which I thought should pass the $Myclass array in when I initialize my class, but apparently not?

What do I need to do to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缘字诀 2024-11-16 10:43:22

啊,我找到了答案,

配置文件中的数组必须名为$config

文件名还必须是库文件名的小写表示形式。

例如

LIB文件:Somelibrary.php
LIB 内容:class Somelibrary($options = array()){...
配置文件:somelibrary.php
配置内容:$config = array('something' => 'value');

AH I found the answer,

The array inside your config file must be called $config.

The name of the file must also be a lower case representation of the library file name.

e.g

LIB FILE: Somelibrary.php
LIB CONTENTS: class Somelibrary($options = array()){...
CONF FILE: somelibrary.php
CONF CONTENTS: $config = array('something' => 'value');

攀登最高峰 2024-11-16 10:43:22

通常的工作方式是传入一组您希望覆盖的选项,或者不传入任何内容以使用默认值。

var myObject = new MyClass(); // default settings

var myObject = new MyClass(array('something' => 'value2')); // override only "something"

老实说,如果没有充分的理由,我不会在配置中创建您自己的文件;相反,只需将默认值放入类定义中,然后在构造函数中覆盖:

class MyClass {

    var $default_options = array(
        'something' => 'value',
        'another' => 'value',
    );
    var $options = array();

    function MyClass($override_options = array())
    {
        $this->options = array_merge($this->default_options, $override_options);

        // your code here...
    }
}

The way this usually works is that you pass in an array of options you wish to override, or pass in nothing to use the defaults.

var myObject = new MyClass(); // default settings

var myObject = new MyClass(array('something' => 'value2')); // override only "something"

Honestly, I wouldn't create your own file in config without a good reason; instead, just put the defaults in your class definition, and then override in your constructor:

class MyClass {

    var $default_options = array(
        'something' => 'value',
        'another' => 'value',
    );
    var $options = array();

    function MyClass($override_options = array())
    {
        $this->options = array_merge($this->default_options, $override_options);

        // your code here...
    }
}
余生共白头 2024-11-16 10:43:22

快进到 2013 年,仍然有人遇到这个问题。所以我的情况是一样的,但略有不同,所以我想我应该尝试拯救别人一些时间。

  1. 我在我的扩展类之后命名我的配置文件,这是错误的。 配置文件应该始终是 form_validation.php,(这是因为最终它会被传递给 CI_Form_validation 类,这就是它所期望的)

  2. 感谢上面回答这个问题的人,我意识到我必须将配置传递给父类,并使用 codeigniter v2.1.3 执行如下操作:

    公共函数 __construct( $config = array() )
    {
      父级::__construct($config);
    }
    

Fast-forward to 2013, someone is still having problems with this. So my situation was the same but slightly different, so I thought I'd try to save someone else some time.

  1. I was naming my config file after my extended class, that was wrong. The Config file should always be form_validation.php, (this is because eventually it is handed to the CI_Form_validation class and that's what it expects)

  2. Thanks to the folks who answered this question above, I realized that I had to pass the config to the parent class and using codeigniter v2.1.3 I did it as follows:

    public function __construct( $config = array() )
    {
      parent::__construct($config);
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文