10

I just read from the book:
"In PHP 5, except for constructors, any derived class must use the same signature when overriding a method"
From the PHP manual in the comments:
"In the overriding, the method names and arguments (arg’s) must be same.
Example:
class P { public function getName(){} }
class C extends P{ public function getName(){} } "

So why I'm able to replace method with other arguments and their quantity? Is this legal or will trigger errors in the future or I'm just missing something?

PHP Version 5.5.11

class Pet {
    protected $_name;
    protected $_status = 'None';
    protected $_petLocation = 'who knows';

// Want to replace this function
    protected function playing($game = 'ball') {
        $this->_status = $this->_type . ' is playing ' . $game;
        return '<br>' . $this->_name . ' started to play a ' . $game;
    }

    public function getPetStatus() {
        return '<br>Status: ' . $this->_status;
    }
}


class Cat extends Pet {

    function __construct() {
        $this->_type = 'Cat';
        echo 'Test: The ' . $this->_type . ' was born ';
    }

// Replacing with this one    
    public function playing($gameType = 'chess', $location = 'backyard') {
        $this->_status = 'playing ' . $gameType . ' in the ' . $location;
        return '<br>' . $this->_type . ' started to play a ' . $gameType . ' in the ' . $location;
    }
}

$cat = new Cat('Billy');
echo $cat->getPetStatus();
echo $cat->playing();
echo $cat->getPetStatus();

This will output:

Test: The Cat was born
Status: None
Cat started to play a chess in the backyard
Status: playing chess in the backyard

2
  • Turn on error displaying and set error level to E_ALL Commented Jun 28, 2014 at 2:51
  • Nothing really happened, it's shows no errors. Commented Jun 28, 2014 at 2:57

2 Answers 2

14

The rule is that a method signature must be compatible with the method it overrides. Let's look at the two methods in your hierarchy:

protected function playing($game = 'ball');

public function playing($gameType = 'chess', $location = 'backyard');

The changes:

  1. Visibility: protected -> public; increasing the visibility is compatible (the opposite will cause errors).

  2. Arguments: no change (same number of required arguments and maximum number of arguments)

Sign up to request clarification or add additional context in comments.

2 Comments

I tried and got this error Declaration of ChildController::test($p1) should be compatible with ParentController::test($p1, $p2 = NULL), so thought I might add that It seems that a child must have minimum no. of arguments as its parent counterpart (or more / or optional but identical in number) @Jack Can you second me ?
@DaniyalNasir that's because your child method can't be called with 2 arguments, hence it's not compatible
5

PHP does not support overloading in the way you are describing it. However, the parameters must only be of the same type (i.e. integers, strings, arrays, etc). You may have additional parameters that are specific to the overriding method, but the initial parameters MUST match those of the parent class.

This may also be a duplicate of PHP function overloading.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.