Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>if you are using a class autoloader, which I highly recommend, you would not want to keep the interface and the class in the same file so that the interface can autoload without needing to first load this one class that implements it.</p> <p>For more info on autoloading: <a href="http://php.net/manual/en/language.oop5.autoload.php" rel="nofollow">http://php.net/manual/en/language.oop5.autoload.php</a></p> <p>another thing you may want to consider is that a given class may impliment multiple interfaces, and multiple classes may implement the same interface.</p> <p>interfaces are primarily used for various design patterns, to enforce rules, and to decouple a class from any dependent classes. when you decouple a class from its dependencies, it makes it much easier to modify code at a later time.</p> <p>for instance, let's say you have a class A that takes in another class B as an argument, and this class is spread throughout your code. you want to enforce that only a class with a specific subset of methods can be accepted as this argument, but you do not want to limit the input to one concrete class and it's decendants. in the future, you may write an entirely different class that does not extend class B, but would be useful as an input for class A. this is why you would use an interface. it is a reusable contract between classes.</p> <p>some would argue that since PHP is a dynamic language, interfaces are an unecessary complication, and that duck typing may be used instead. I find in large multi-user code bases however, that interfaces can save a lot of time, letting you know more about how one class uses another, without having to study the code in depth.</p> <p>if you find yourself with a large list of variables that you have to pass around between objects or functions, they often do end up deserving a class of their own, but each case is different.</p> <p>-- dependency injection example --</p> <pre><code>class A implements AInterface { public function foo($some_var) {} } interface AInterface { public function foo($some_var); } class B { protected $localProperty; // inject into the constructer. usually used if the object is saved in a property and used throughout the class public function __construct(AInterface $a_object) { $this-&gt;localProperty = $a_object; } // inject into a method. usually used if the object is only needed for this particular method public function someMethod(AInterface $a_object) { $a_object-&gt;foo('some_var'); } } </code></pre> <p>you can now see that you can write another class that impliments a foo method (and the AInterface) and use that within class B as well.</p> <p>as a real world example (used often), say you have a database class with particular methods that interact with the database (getRecord, deleteRecord). now lets say at a later time you find a reason to switch database rdbms. you now need to use entirely different SQL statements to accomplish the same goals, but since you used an interface for your type hinting, you can simply create a new class that impliments that interface, but impliments those same methods in entirely different ways as it interacts with a different rdbms. when creating this new class, you will know exactly what methods need to be written for this new class in order to fit into the same objects that need to use a database object. if you use a container class that you use to create objects and inject them into other objects, you would not need to change too much application code in order to switch databases classes, and therefore switch database rdbms. you could even use a factory class, which could limit your changes to one line of code to make this type of change (in theory).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload