2

in laravel 5.4 we can bind a class which known and we use it anywhere after binded but if we wants bind some classes implemented specific interface how we bind dynamically ?

for example:

interface x {}

class a implements x {}
class b implements x {}
class c implements x {}
class d implements x {}
class e implements x {}

in service provider how can i bind all classes implemented x dynamicaly so in future i will add some class implemeted x. how can i do?

9
  • 1
    Check this post laravel.com/docs/5.4/… , $this->bind('x', 'a') should do the trick, but I don't understand why you need it, cause after every binding a new binded class will be used. Feb 18, 2019 at 19:53
  • i will make a notify structure should managed in panel it means when i added a new class implement x it must shown on notify type list in panel and i can use it without does not add anything. ok ?
    – Hanik
    Feb 18, 2019 at 19:58
  • i need access all classes implemented x dynamically
    – Hanik
    Feb 18, 2019 at 19:59
  • ok what about contextual binding laravel.com/docs/5.4/container#contextual-binding ? Feb 18, 2019 at 20:09
  • 1
    Ok, understood, then you would have to use scandir($dir); to get list of files, get the name of each file, concatenate it with namespace and use instanceof to check if this class implements needed interface Feb 18, 2019 at 20:28

0

Your Answer

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

Browse other questions tagged or ask your own question.