奇怪的数组转换为布尔

时间:2015-12-29 10:25:46

标签: php

我提出这个问题,因为我将一个奇怪的数组转换为布尔值,我真的不知道该怎么回事。

我已经仔细检查了我的代码,但我找不到任何可以修改代码的问题。

你看错了吗?如果是这样,你能帮帮我吗?

因此,根据要求,我将更详细地解释我的问题。

所以,我有这个类,我有一个名为load_configuration()的方法,它加载一些php文件,每个都返回一个值数组。

这些文件返回的数组存储在类的等效属性中。

load_configuration()方法中,我为我的班级属性var_dumpfieldssettings,我得到以下结果:

array (size=1)
  'text' => 
    array (size=3)
      'type' => string 'text' (length=4)
      'label' => string 'Hello world! goes here.' (length=23)
      'default' => string 'Hello world!' (length=12)

另外,我在方法load_configuration()的末尾创建了一个数组,然后返回数组。

然后当我尝试使用方法属性或方法load_configuration()中返回的值时,我得到以下var_dump()

// In case of the returned array
array (size=2)
  'fields' => boolean true
  'settings' => boolean true

// In case of the class property
boolean true

但我不明白这个原因。这是一个非常奇怪的修改。

为了更好地理解,请查看方法load_configuration()get_template_variables()

中的注释
class SS24_Widget extends \SiteOrigin_Widget {
    protected $config_folder = 'Config';
    protected $form_settings = 'settings';
    protected $form_fields = 'fields';
    protected $fields = array();
    protected $settings = array();

    public function __construct( $unique_widget_id = '', $widget_name = '' ) {

        if ( empty( $unique_widget_id ) ) {
            $unique_widget_id = uniqid( 'widget-' );
        }

        $this->load_configuration();

        parent::__construct(
            $unique_widget_id, // The unique id for your widget.
            $widget_name,      // The name of the widget for display purposes.
            $this->settings,   // The widget settings.
            array(),           // The $control_options array, which is passed through to WP_Widget.
            $this->fields,     // The widget fields.
            $this->get_dir() . '/'  // The $base_folder path string.
        );
    }

    protected function load_configuration() {

        $config_files = $this->get_dir() . '/' . $this->config_folder . '/*.php';

        $fields = array();
        $settings = array();

        foreach ( glob( $config_files ) as $file ) {
            switch( basename( $file, '.php' ) ) {
                case $this->form_settings:
                    $this->settings = $this->{$this->form_settings} = require_once $file;
                    $settings = $this->settings;
                    break;
                case $this->form_fields:
                    $this->fields = $this->{$this->form_fields} = require_once $file;
                    $fields = $this->fields;
                    break;
            }
        }

        // This print out the following:
        // array (size=1)
        //  'text' => 
        //    array (size=3)
        //      'type' => string 'text' (length=4)
        //      'label' => string 'Hello world! goes here.' (length=23)
        //      'default' => string 'Hello world!' (length=12)
        var_dump($fields);

        return array(
            'fields'   => $fields,
            'settings' => $this->settings,
        );
    }

    // ...

    public function get_template_variables( $instance, $args ){

        $c = $this->load_configuration();

        // While the following printint out the following:
        // array (size=2)
        //  'fields' => boolean true
        //  'settings' => boolean true
        //
        // boolean true
        var_dump($c);
        echo "<br />";
        var_dump($this->fields);

        return $variables;
    }
}

有人可以解释一下这段代码可能有什么问题吗?

更新#1 我发现父类有一个名为$fields的受保护成员属性,因此在我的本地代码中,我现在将变量更改为fields_settings,但仍然转换为boolean

2 个答案:

答案 0 :(得分:1)

我认为问题是你在构造函数中执行load_configuration(),所以require_once语句在这里返回正确的值。在下一次调用load_configuration()时,require_once语句返回boolean&#34; true&#34;,表示该文件已被包含。

使用require而不是require_once。

答案 1 :(得分:1)

require_onceinclude_once非常相似。

如果已经包含文件,则它们都将返回布尔 TRUE

引用PHP documentation

  

如果已包含文件中的代码,则不会再次包含该文件,并且include_once将返回 TRUE 。顾名思义,该文件只包含一次。

require_once指向include_once的文档。这就是该块提到前者的原因。

要解决您的问题,请确保使用require代替。