user1794918 user1794918 - 1 year ago 116
PHP Question

Declaration of Child method should be compatible with Parent method

Warning: Declaration of Child::default_action($form_id) should be compatible with Parent::default_action()

I know I lack in the department of understanding object coding and I luck up fixing things some times but in this message I am try to understand why something worked.

I was getting the error message above in the php error log. The original parent method is

function default_action() {
echo "<html><body></body></html>";

I changed it to

function set_default_action() {
echo "<html><body></body></html>";

The error messages in the log went away. Now I am trying to find out why did this work. I put it there because I saw in the code set_current_action. I kind of have an idea that set_ get_ some others are commands that precede the method declaration or something like that.

Answer Source

The problem is that the function declaration for the parent and child are not compatible (like the error says)

public function method([$args]) is the function declaration.

In your case the parent method does not take any parameters but the child method does, so you get the error.

To solve this you should have the parent method accept the same parameters as the child method. You will probably want to provide some sensible default for the parameters.

//parent class
function default_action($form_id = null) {

As RiggsFolly points out, you may not have control over the parent class and have to make your child class conform. In that case you can use a class property to pass the form id to the method.

//Child class

class Child extends Parent{
    public $form_id;

    function setFormId($form_id){
      $this->form_id = $form_id;

    function default_action(){
        //Work with $this->form_id


//In code
$form_id = 123;
$C = new Child();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download