oop – Unexpected warnings received from Theme Options Code

Actually, I've created my own class for registering theme options, and it works fine, but when I started to take some precautions, warnings were displayed and I can not believe what I did wrong.

I will explain the old and new code so that you understand the problem better.

Work

The following constructor accepts three arrays, representing menu items, settings (sections / fields), and widgets.

public function __construct($menu = array(), $sections = array(), $widgets = array())

Within the constructor, I started setting properties and calling required hooks for the settings page, which should register settings and fields using appropriate callbacks. Each field should be rendered dynamically according to its class name $render = new $field_class($field, $value, $this); (Each field has its own class that extends the class of theme settings, and I'll show it later.)

The field rendering 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 contains the field data (eg id, label, type), $value the option corresponds to the field ID and an object of the parent class $this,

Then in the field-specific class the following code. (The problem is in 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 panel expands the theme setting class.

It does not work

So far everything is fine, but when I started moving some lines from the parent class (field render method) to the field-specific constructor, warnings were displayed.

That's how the render function became

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 that I get:

Warning: Illegal string offset & # 39; id & # 39 ;.

Note: Uninitialized string offset: 0.

while I've just moved the code from the parent to the child.

I do not know if I can explain what happened so you can understand the problem. but I hope you can help, and I'm sure I'm ready to explain more if needed. Thank you in advance.

Note

Warnings are displayed, but everything is displayed correctly. This may mean something is called twice, but no values ​​could be found the first time.