Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Overriding methods rules

Tags:

php

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

like image 972
ProgZi Avatar asked Jun 28 '14 02:06

ProgZi


People also ask

Can you override methods in PHP?

Introduction to the PHP overriding method Method overriding allows a child class to provide a specific implementation of a method already provided by its parent class. To override a method, you redefine that method in the child class with the same name, parameters, and return type.

What is function overriding in PHP?

Function Overriding It is the same as other OOPs programming languages. In this function, both parent and child classes should have the same function name and number of arguments. It is used to replace the parent method in child class.

How can we call parent class method in overriding in PHP?

class Foo { $text = "world\n"; protected function test() { echo $this->text; } }// class Foo class Bar extends Foo { public function test() { echo "Hello, "; // Cannot use 'parent::test()' because, in this case, // Foo::test() requires object data from $this parent::test(); } }// class Bar extends Foo $x = new Bar; $x- ...


2 Answers

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)

like image 180
Ja͢ck Avatar answered Oct 07 '22 14:10

Ja͢ck


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.

like image 40
jsickles Avatar answered Oct 07 '22 12:10

jsickles