Php 奇怪的数组到布尔值的转换

Php 奇怪的数组到布尔值的转换,php,Php,我提出这个问题,因为我有一个奇怪的数组到布尔值的转换,我真的不知道这有什么错 我已经非常仔细地检查了我的代码,没有发现任何可以修改代码的问题 你觉得有什么不对劲吗?如果是这样,你能帮我吗 因此,根据要求,我将更详细地解释我的问题 我有一个类,我有一个名为load_configuration的方法,它加载一些php文件,每个文件返回一个值数组 这些文件返回的数组存储在类的等效属性中 在load_配置方法中,我对我的类属性字段和设置执行var_转储,得到以下结果: array (size=1)

我提出这个问题,因为我有一个奇怪的数组到布尔值的转换,我真的不知道这有什么错

我已经非常仔细地检查了我的代码,没有发现任何可以修改代码的问题

你觉得有什么不对劲吗?如果是这样,你能帮我吗

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

我有一个类,我有一个名为load_configuration的方法,它加载一些php文件,每个文件返回一个值数组

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

在load_配置方法中,我对我的类属性字段和设置执行var_转储,得到以下结果:

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_配置的末尾创建了一个数组,并返回了数组

然后,当我尝试使用方法属性或方法load\u配置返回的值时,我通过var\u dump得到以下结果

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

为了更好地理解,请查看方法load\u configuration和get\u template\u变量中的注释

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

更新1
我发现父类有一个名为$fields的受保护成员属性,因此在我的本地代码中,我现在将变量更改为一个fields\u设置,但仍然转换为布尔值。

我认为问题在于您在构造函数中执行load\u配置,因此require\u once语句在此处返回正确的值。在下一次调用load_configuration时,require_once语句返回boolean true,表示文件已经包含在内

使用require而不是require\u once。

require\u once与include\u once非常相似

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

引述:

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

记录需要包含一次的点。这就是为什么block提到了前者


要解决您的问题,请确保改用require。

是否可以解释您投反对票的原因?您的问题是什么?我的意思是,你为什么要问这个问题,你希望用这个代码得到什么,你得到什么?张贴一段代码并询问您对它的看法很难回答。为此,您可以使用php解释器:您是否阅读了我代码上面的注释?你也看过代码了吗?如果你仔细阅读,你会发现问题和问题所在。我发现你的帖子有点让人困惑,你能把它简化一点,更具体地说明错误在哪里吗?好的,如果这是你的问题,我将写下问题所在:
// In case of the returned array
array (size=2)
  'fields' => boolean true
  'settings' => boolean true

// In case of the class property
boolean true
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;
    }
}