PHP Overriding methods rules -


i read book:
"in php 5, except constructors, derived class must use same signature when overriding method"
php manual in comments:
"in overriding, method names , arguments (arg’s) must same.
example:
class p { public function getname(){} }
class c extends p{ public function getname(){} } "

why i'm able replace method other arguments , quantity? legal or trigger errors in future or i'm missing something?

php version 5.5.11

class pet {     protected $_name;     protected $_status = 'none';     protected $_petlocation = 'who knows';  // want replace function     protected function playing($game = 'ball') {         $this->_status = $this->_type . ' playing ' . $game;         return '<br>' . $this->_name . ' started play ' . $game;     }      public function getpetstatus() {         return '<br>status: ' . $this->_status;     } }   class cat extends pet {      function __construct() {         $this->_type = 'cat';         echo 'test: ' . $this->_type . ' born ';     }  // replacing 1         public function playing($gametype = 'chess', $location = 'backyard') {         $this->_status = 'playing ' . $gametype . ' in ' . $location;         return '<br>' . $this->_type . ' started play ' . $gametype . ' in ' . $location;     } }  $cat = new cat('billy'); echo $cat->getpetstatus(); echo $cat->playing(); echo $cat->getpetstatus(); 

output:

test: cat born
status: none
cat started play chess in backyard
status: playing chess in backyard

the rule method signature must compatible method overrides. let's @ 2 methods in hierarchy:

protected function playing($game = 'ball');  public function playing($gametype = 'chess', $location = 'backyard'); 

the changes:

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

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


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jquery - Keeping Kendo Datepicker in min/max range -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -