class - How to create a custom type declaration(type hinting) in PHP -


i have seen in few mvc frameworks

class hotel{  function add(addrequest $post){ $this->save($post->all()); }  } 

i create own type hinting using php class bellow , know there 2 ways it.

with class:

public function myfunction(someclass $instance) 

with interface:

public function myfunction(someinterface $instance) 

i couldn't find article it.can explain briefly on how create our own type hinting , advantages doing so.

the first benefit of using type hinting validation , self-documentation comes i.e. if come code haven't seen in while or working on else's code able see straight away must passed instance of specific. second being, if not pass required instance throw error. (this without code)

furthermore, use of interfaces allows different implementations of class passed through code allow because interface should declare required methods (think different db drivers).

i've not used yii before don't know it. laravel, on other hand, works using reflection , injects instance of class require. called dependency injection.

the link below video explain concept in more detail: https://laracasts.com/series/laravel-5-from-scratch/episodes/16

hope helps!


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -