Jump to content
Lizard Wizard Coding Club

Override Parent Class


Recommended Posts

I need to add more code to an existing function located inside the parent class. I'm using code like this but can't get it working :

 

// Parent

class Default_Fields {

    public function fields() {

        $this->_fields = array()
            
        // Default Code
    }

}


// Child

class More_Fields extends Default_Fields {

    public function fields() {

        $this->_fields = array()
            
        // Modified Code
    }

}


$new = new More_Fields;

$new->fields();

The modified code in the child class is an extended version of whats in the parent. There's no errors but the additional code is not printing. 

I can link to the complete file containing all the parent code if needed.

Link to post
Share on other sites
23 hours ago, administrator said:

What error message are you getting?

No error message, just no changes. The child class is NOT adding the new code. I'm trying to add code to an existing public function using a child class.

 

I need to extend a parent class to add an extra field.

This is the parent class.

class Default_Fields {

public function fields() {

    $this->_fields = array()
        
   array(
            'name'        => 'text_one',
            'label'       => __( 'Text One' ),
            'type'        => 'text',
        ),
        
    );    
}

}

This is the child class

class More_Fields extends Default_Fields {

public function fields() {

    $this->_fields = array()
        
    array(
            'name'        => 'text_one',
            'label'       => __( 'Text One' ),
            'type'        => 'text',
        ),
    array(
            'name'        => 'text_two',
            'label'       => __( 'Text Two' ),
            'type'        => 'text',
        ),
    );    
}

}


$new = new More_Fields;

$new->fields();

The additional code in the child class for Text Two is not added. Not sure why. What am i missing or is there another method?

Edited by haymanpl
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...