Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between a function layer and an interface
    primarykey
    data
    text
    <p>What's the difference between doing this:</p> <pre><code>class UserRepository extends DAO { public function GetAllUsers() { return $this-&gt;Select(); } public function GetUserById( $id ) { return $this-&gt;Select( $id ); } } class MockUserRepository extends MockDAO { public function GetAllUsers() { return $this-&gt;Select(); } public function GetUserById( $id ) { return $this-&gt;Select( $id ); } } class UserController { private $userRepository; function __Construct( $repo ) { $this-&gt;userRepository = $repo; } public function ListUsers() { $users = $this-&gt;userRepository-&gt;AnUndeclaredMethod(); // ... } } $repo = new MockUserRepository(); $controller = new UserController( $repo ); </code></pre> <p>and this:</p> <pre><code>interface IUserRepository { public function GetAllUsers(); public function GetUserById( $id ); } class UserRepository extends DAO implements IUserRepository { public function GetAllUsers() { return $this-&gt;Select(); } public function GetUserById( $id ) { return $this-&gt;Select( $id ); } } class MockUserRepository extends MockDAO implements IUserRepository { public function GetAllUsers() { return $this-&gt;SelectUsers(); } public function GetUserById( $id ) { return $this-&gt;SelectUsers( $id ); } } class UserController { private $userRepository; public function __Construct( IUserRepository $repo ) { $this-&gt;userRepository = $repo; } public function ListUsers() { $users = $this-&gt;userRepository-&gt;AnUndeclaredMethod(); // ... } } $repo = new MockUserRepository(); $controller = new UserController( $repo ); </code></pre> <p>If a function was used in the controller that wasn't declared in one of the repositories, it would throw an error anyway, why is it better to use an interface for this type of task? What do I benefit from?</p> <ol> <li>Is it all about the error? if you forget to implement one function you know must exist, it errors regardless of using it or not, is that the benefit of the interface?</li> <li>Is it about the fact that you may need 2 repositories for 2 data sources and so without knowing your application would just stop working if your controller called upon the function that wasn't implemented rather than straight away alerting you to the fact one of your repositories is not implementing the methods you have told it to in the interface? Once again, is it all about the errors?</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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