0

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.

5
  • Not sure if this helps, but your child constructor is missing the $value parameter - function __construct( $field = array(), $parent = NULL ) - it should be function __construct( $field = array(), $value ='', $parent = NULL ) ? And did you try var_dump( $field ); to see it contains proper keys/values?
    – Sally CJ
    Commented Aug 5, 2019 at 10:20
  • Thank you, but no, the object is instantiated without the value parameter new $field_class($field, $this)
    – Makiomar
    Commented Aug 5, 2019 at 10:23
  • But I can see $render = new $field_class($field, $value, $this); in the code and your question text as well... And what's the output of the var_dump()?
    – Sally CJ
    Commented Aug 5, 2019 at 10:29
  • I think you are missing something.. if you checked the snippets in the not working code you will find that everything is proper.. you are looking on the working snippets
    – Makiomar
    Commented Aug 5, 2019 at 10:33
  • Ok, maybe I am... but how about just try to add that $value parameter and instantiate the class with three parameters? And you still have not answered my question on the var_dump thing. Try it and inspect the output.
    – Sally CJ
    Commented Aug 5, 2019 at 10:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.