Author - StudySection Post Views - 1,316 views
Service Layer | PHP

Service Layer in PHP

Service Layer is a list of service classes that are used to handle application logic. Service classes are distinguished from used from different front ends.

At every level of abstraction, Source code is used to represent the data entities and their behavior. In object-oriented programming, there are different types of logic that can be partitioned into

  • Application logic: It is the scope of a service layer and it is not domain-specific. For example, converting objects into JSON or XML is a function of application logic. It can even be used with application-specific classes. The task of showing data in a particular format is unobserved to the domain. As it is not domain-specific.
  • Business logic: It is encapsulated in a domain model. And it is specific to the particular domain the application works in. In an enterprise application, all added value depends on business logic.
  • Presentation logic: Presentation logic is considered as the subset of application logic. Which handled the end-user view of the application.

All those different aspects of an application are organized into different layers. Where each layer resides on the top of the previous layer. In older approaches, there is an infrastructure layer on which the business logic depends and which can deal with persistent issues.

A Service Layer is useful when there are multiple front-ends to a common domain model. These interfaces or APIs delegate the application logic to the service layer. Which may encapsulate each other.

Responsibility of Service Layer:- It includes the responsibility of CRUD function(s) over the object of a specific domain model. Some of the responsibilities are already included while others are only specified as interfaces.

Example code

It includes two service classes one as the implementation of the interface of the domain model and another is only employed at a higher level

<?php
require_once 'domain.php';
/**
* Adapter for an interface defined in the lower layer
*/
class EmailRepositoryClass implements EmailRepository
{
public function getAllEmail($address)
{
// fake implementation
$mail = new Email();
$mail->setSender(sunil@example.com');
$mail->setRecipient(sunilseth9i@example.com');
$mail->setSubject('testing');
$mail->setText('it is just testing the class’');
return array(
$mail
);
}
}
/**
* Service that belongs only to the very Service Layer.
* It may have an interface, but it will be kept in this layer.
*/
class EmailTransferService
{
/**
* Transforming an object to XML is a common task for a Service Layer.
* However, dependencies towards the Domain Model are well-accepted
* but they don't mean that this layer's classes should be
* included in the lower layer.
* Keeping the XML transformation in one place aids different front-end
*/
public function toXml(Email $mail)
{
return "<mail>\n"
. " <sender>" . $mail->getSender() . "</sender>\n"
. " <recipient>" . $mail->getRecipient() . "</recipient>\n"
. " <subject>" . $this->_encode($mail->getSubject()) . "</subject>\n"
. " <text>" . $this->_encode($mail->getText()) . "</text>\n"
. "</mail>\n";
}
protected function _encode($text)
{
return htmlentities($text);
}
}
// client code
$repository = new EmailRepositoryClass();
$transferService = new EmailTransferService();
foreach ($repository->getAllEmail('bob@example.com') as $mail) {
echo $transferService->toXml($mail);
}

English is a commonly used language in almost all fields today. Having a certification in the English language can enhance your career aspects. StudySection offers both basic level and advanced level English certification exams to test and certify the English language skills.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri