inheritance - PHP - calling class that implements interface without knowing class name -


I am trying to create a PHP file that calls functions to another file. Some sample code:

Interface code:

  Interface AJAXDispatcher {Fixed function send ($ action); }  

implementation:

  class myAJAX applied AJAXDispatcher {fixed function send ($ action) {if ($ action === "action1") {do_something (); }  

I think it's okay I try to call it by importing the file first. I'm trying to make it independent of the class name so that I can do something like this:

  AJAXDispatcher :: dispatch ($ action);  

Thought that this will work because my AJAX will be the successor to AJAXDispatcher, but I get the following error:

  Fatal error: abstract method AJAXDispatcher :: dispatch Can not call () in ....  

Anyone knows what I am doing?

Interfaces do not make sense in a stable way, because to call a static method (usually But) need to know the name of the class.

Instead, you have a non-static method and an example of my AJAX code that calls the AJAXDispatcher and receives an example and calls it. You can use type hinting to make sure that you are getting the correct example.

  Interface AJAXDispatcher {public function dispatch ($ action); } Class myAJAX AJAXDispatcher {public function despatch ($ action) {do_something (); }} Class Controller {Private $ Sender; Public Function __ Composition (AJAXDispatcher $ Dispatcher) {$ this- & gt; Dispatcher = $ sender; } Public Function Action ($ verb) {$ this- & gt; Dispatcher- & gt; Despatch ($ action); }} $ Sender = new myAJAX (); $ Controller = new controller ($ sender); $ Controller- & gt; Action ('index');  

This example uses the design pattern.


Comments