admin管理员组文章数量:1418400
Actually i have built my own class for registering theme options, and it works fine, but once i started to make some arrangements warnings started to show and i can't catch what wrong have i done.
I will explain the old and new code to help you understand the problem.
Working
The following constructor accepts three arrays that represents menu items, settings (sections/fields) and widgets.
public function __construct($menu = array(), $sections = array(), $widgets = array())
within the constructor i started to set properties and call required hooks for settings page, which should register settings and fields using corresponding callbacks. each field should be rendered dynamically according to its class name, something like $render = new $field_class($field, $value, $this);
(Each field has it's own class that extends the theme settings class. i will show you later).
The field render method
/**
* class section fields callback function
*/
public function field_input($field){
if(isset($field['callback']) && function_exists($field['callback'])){
}
if(isset($field['type'])){
$field_class = 'ANONY_optf__'.ucfirst($field['type']);
if(class_exists($field_class)){
$fieldID = $field['id'];
$fieldDefault = isset($field['default']) ? $field['default'] : '';
$value = (isset($this->options->$fieldID))? $this->options->$fieldID : $fieldDefault;
$render = new $field_class($field, $value, $this);
$render->render();
}
}
}
Each field class accepts three arguments, $field
that contains field data (e.g. id, label, type), $value
of the option corresponds to the field id and an object of parent class $this
.
Then in the field specific class, the following code. (The problem is within the constructor so i just wrote it)
class ANONY_optf__Switch extends ANONY__Theme_Settings{
function __construct( $field = array(), $value ='', $parent = NULL ){
if( is_object($parent) ) parent::__construct($parent->sections, $parent->args);
$this->field = $field;
$this->value = $value;
}
}
As you can see the switch field extends theme setting class.
Not working
until here every thing is fine, but once i started to move some lines from parent class (Field render method) to the field specific constructor, warnings started to show.
So the render function became like this
public function field_input($field){
if(isset($field['callback']) && function_exists($field['callback'])){
}
//Array of inputs that have same HTML markup
$mixed_types = ['text','number','email', 'password','url'];
if(isset($field['type'])){
$field_class = 'ANONY_optf__'.ucfirst($field['type']);
//Static class name for inputs that have same HTML markup
if(in_array($field['type'], $mixed_types)) $field_class = 'ANONY_optf__Mixed';
if(class_exists($field_class)){
$field = new $field_class($field, $this);
$field->render();
}
}
}
And the field specific constructor:
function __construct( $field = array(), $parent = NULL ){
if( is_object($parent) ) parent::__construct($parent->sections, $parent->args, $parent->widgets);
$this->field = $field;
$fieldID = $this->field['id'];
$fieldDefault = isset($this->field['default']) ? $this->field['default'] : '';
$this->value = (isset($parent->options->$fieldID))? $parent->options->$fieldID : $fieldDefault;
}
After this i am getting:
Warning: Illegal string offset 'id'.
Notice: Uninitialized string offset: 0.
while i've just moved the code from parent to child.
I don't know if could explain what has happened, so you can understand the problem. but i hope you can help, and sure i am ready to explain more if needed. Thanks in advance.
Note
Warnings are showing but also everything is rendered correctly. Which it may mean that something is called twice but first time it couldn't find values.
本文标签: oopGetting unexpected warnings from theme options code
版权声明:本文标题:oop - Getting unexpected warnings from theme options code 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745271398a2650912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论