Answer the question
In order to leave comments, you need to log in
Is it normal to access methods and properties of classes through a single service?
Good afternoon!
There is a service class through which the properties and methods of other classes (models) are accessed. Implemented like this:
namespace App\Services;
use App\Models\City;
use App\Models\Country;
use App\Models\Hotel;
use App\Models\Region;
class Service
{
private Country $country;
private City $city;
private Hotel $hotel;
private Region $region
/**
* @param $name
* @return City|Country|Hotel|Region
* @throws \Exception
*/
public function __get($name)
{
switch ($name) {
case 'country':
return new Country();
case 'city':
return new City();
case 'hotel':
return new Hotel();
case 'region':
return new Region();
default:
throw new \Exception('Property ' . $name . ' is not exist.');
}
}
}
$service = new Service();
$some = $service->city->someMethod();
$field_name = $service->country->field_name;
Answer the question
In order to leave comments, you need to log in
From my belfry of static languages, such a construction is excessive complexity from scratch.
But there are places where such complexity is also somehow justified. For example, Qt - the contents of a table cell ...
QVariant SomeModel::data(
const QModelIndex &index, int role = Qt::DisplayRole) const override {}
congratulations, you discovered the facade design pattern
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question