The Clean Architecture in php


interface CustomerRepositoryInterface { public function



Download 2,26 Mb.
Pdf ko'rish
bet82/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   78   79   80   81   82   83   84   85   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

interface
CustomerRepositoryInterface {
public function
getById
(
$id
);
}
We have a simple interface with one method,
getById()
, which returns a
Customer
object for the
customer data identified by
$id
. As this is an interface, we cannot provide any implementation
details, so this class provides no information about where the data comes from, or how it is
retrieved.
Now in our controller, we use PHP’s type-hints for methods and functions to declare that
the argument passed to the
__construct()
method must be an instance of our new interface,
CustomerRepositoryInterface
.
class
CustomerController
{
protected
$repository
;
public function
__construct
(CustomerRepositoryInterface
$repo
) {
$this
->
repository
=
$repo
;
}
public function
viewAction
() {
$customer
=
$this
->
repository
->
getById
(
1001
);
return
$customer
;
}
}
Now the
CustomerController
class is only coupled to the
CustomerRepositoryInterface
,
but that’s okay: this interface isn’t a concrete implementation, it’s just a definition of an
implementation. We can couple to this and, in fact, we should, as it defines how our application
interacts, without referencing the specific implementations.
Whatever mechanism is responsible for instantiating the
CustomerController
can still provide
it with the concrete
CustomerRepository
, so long as that class implements the ‘CustomerRepos-
itoryInterface’.


Defining a Contract with Interfaces
55

Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   78   79   80   81   82   83   84   85   ...   179




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish