我之所以提出这个问题,是因为我将数组奇怪地转换为布尔值,我真的不知道这有什么问题。
我已经非常仔细地检查了我的代码,没有发现任何可以修改我的代码的问题。
你看到什么不对吗?如果是的话,你能帮我吗?
因此,根据要求,我将更详细地解释我的问题。
所以,我有了这个类,我有一个名为load_configuration()
的方法,它加载一些php文件,每个文件返回一个值数组。
这些文件返回的数组存储在类的等效属性中。
在load_configuration()
方法中,我为类属性fields
和settings
执行var_dump
,得到以下结果:
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
。
require_once
与include_once
非常相似。
如果已经包含一个文件,它们都将返回布尔值TRUE。
引用PHP文档:
如果已包含文件中的代码,则不会再次包含该代码,include_one将返回TRUE。顾名思义,该文件将只包含一次。
require_once
的文档指向include_once
。这就是为什么区块提到前者的原因。
要解决您的问题,请确保使用require
。
我认为问题在于您在构造函数中执行load_configuration()
,因此require_once
语句在这里返回正确的值。在对load_configuration()
的下一次调用中,require_once
语句返回布尔值"true",表示文件已包含在内。
使用require而不是require_one。