oop - polymorphism + interfaces in php -


consider have code: (result "works")

interface iuser { } interface irepository {     function save(iuser $user); }  class user implements iuser {  }  class repository implements irepository {     # works iuser why not user? user implements iuser :)     function save(iuser $user) {          if(!$user instanceof user)             throw new \invalidargumentexception('...');          echo 'works!';     } }  $user = new user(); $repo = new repository(); $repo->save($user); 

this example works because user implements iuser. if implement save function way:

function save(user $user) { 

it fails message declaration of repository::save() must compatible of irepository::save()

from point of view isn't true. user compactible iuser.

  1. why php doesnt support this? there real reason or more bug in language?
  2. where difference in typechecking between typehint , instanceof?
  3. how other languages handels this?

you have change iuser user in save method in interface irepository too. interface method must compatible implementation method. that's all, no language issue or similar... in other (compiled) languages (e.g. c#, java) error before compilation.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -