Monday, 19 August 2013

PHP and C#.NET MVC ( repository pattern in PHP, how might i go about it )

PHP and C#.NET MVC ( repository pattern in PHP, how might i go about it )

I love the Unity and Ninject framework with dependency injection for
C#.NET into the controllers with repository interfaces etc, but I am
trying to think up an alternative in PHP and am struggling.
I have:
class User
{
//..
}
interface IUserRepository
{
public function Repository(); // This can't work as a variable, should
I abstract?
}
class UserRepository implements IUserRepository
{
public function Repository()
{
$users = // DAO gets users and returns them..
// But how does the IUserRepository help me at all? Even if I had
ninject or
// Unity creating some new link between the UserRepo and IUserRepo?
return $users;
}
public function GetAllUsers()
{
// errr.. I'm confused
}
}
// Something has to say if the AdminController is called,
// inject into its construct, a new IUserRepository
class AdminController extends Controller
{
private $repository;
public function __Construct( $repository )
{
$this->repository = $repository;
}
public function ActionResult_ListUsers()
{
$users = $repository->GetAllUsers();
// Do some clever View method thing with $users as the model
}
}
The real question is PHP and repository approach. How does that work? You
can see I'm blotching this a bit and really want to get it right!
EDIT::
Even more simple question. What is the benefit of the interface keyword?
How does declaring an interface with some methods undefined and then
making a new class that implements those methods to extend them, help when
you cannot create a new interface like a class that then is filled in with
the proper class that defines them?

No comments:

Post a Comment