Support the ongoing development of Laravel.io →
posted 10 years ago
Laravel.io
Last updated 2 years ago.
0

For my personal taste i better like when Interface and all its concrente implementations are together in one folder, it is later easier to locate them all and see all possible implementations.

And i would name them a little bit different:


// Interface
Acme/Repositories/UserRepository

// Concrete implementations
Acme/Repositories/EloquentUserRepository
Acme/Repositories/MongoUserRepository
Acme/Repositories/SomeRemoteServiceUserRepository

But it is just me. Some say that append interfaces with word 'Interface' is betters, because you can quickly find all interfaces + just by reading file name you can know that it is an interface.

// HomeController.php

// you can only use interface, you don't need to additionally include concrete class that will be used, so you can remove fallowing:
use Acme\repositories\UserRepository as UserRepository;

// You can use 'use' like so if class name does not change
use Acme\interfaces\UserInterface;

// When to use as suffix
use Acme\interfaces\UserInterface as MyDifferentName;

// Use capitalized namespaces
use Acme\Interfaces\UserInterface;

Last updated 2 years ago.
0

Good start with Laravel! JMO, my suggestions would be to

  • Inject the DB and User classes into the UserRepository.
  • rename UserInterface to UserRepositoryInterface,
  • create a new UserInterface for the User class then use that to typehint the dependency injection into the UserRepository construct.

A refactored UserRepository would look something like this:

namespace Acme\repositories;

use DB;

class UserRepository implements UserRepositoryInterface
{
	protected $db;
	protected $user;

	public function __construct(DB db, UserInterface $user)
	{
		$this->db = $db;
		$this->user = $user;
	}
	
	public function listUsers()
	{
		return $this->user->all();
	}
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.