如何停止传递null给' __construct '



我有一个在Laravel应用程序中的Controller内部调用的Service。通常这些服务是使用组合模式实现的,其中服务被注入到控制器的__construct中;然后,当需要使用服务时,你只需调用你想要的服务和方法。如果您想在数据库上创建一个新的注册表,您只需要调用服务、方法,并将必要的参数传递给该方法。

但是假设我不想传递参数。我想要一个不可变的,零参数的对象。我试图在下面的代码中实现这一点(首先是服务,然后是调用服务的控制器)。注意,在我的服务的构造函数中,我有需要传递给每个方法的所有参数。这就产生了我想要的:一个不可变的、零参数的对象。

但是有一个问题:并不是在每次调用服务时,我都需要构造函数中的所有参数(并不是每个使用的方法都需要它们),这就是为什么它们的类型为[any type]|null。这似乎是错误的,因为如果我不需要所有的参数来构造我的对象,也许它应该是另一个对象。我对小班没有问题(我喜欢它,实际上),但似乎有CreateCmsUserService,UpdateCmsUserService,DeleteCmsUserService太多了。

我想知道这个构造函数是否有一个优雅的解决方案为null,或者如果唯一的方法是划分类。

<?php
namespace AppServices;
use AppModelsUser;
use Exception;
use IlluminateSupportFacadesHash;
use AppInterfacesCRUD;
class CmsUsersService implements CRUD
{
private ?int $user_id;
private ?array $data;
private ?string $users_to_be_deleted;
public function __construct(?array $data, ?int $user_id, ?string $users_to_be_deleted)
{
$this->user_id = $user_id;
$this->data = $data;
$this->users_to_be_deleted = $users_to_be_deleted;
}
public function create()
{
$this->data['token'] = Hash::make($this->data['email']);
$this->data['password'] = Hash::make($this->data['password']);
User::create($this->data);
return cms_response(trans('cms.users.success_create'));
}
public function update()
{
try {
if (array_key_exists('password', $this->data)) {
$this->data['password'] = Hash::make($this->data['password']);
}
$user = $this->__findOrFail();
$user->update($this->data);
return cms_response(trans('cms.users.success_update'));
} catch (Throwable $th) {
return cms_response($th->getMessage(), false, 400);
}
}
public function delete()
{
User::whereIn('id', json_decode($this->users_to_be_deleted))->delete();
return cms_response(trans('cms.users.success_delete'));
}
private function __findOrFail()
{
$user = User::find($this->user_id);
if ($user instanceof User) {
return $user;
}
throw new Exception(trans('cms.users.error_user_not_found'));
}
}
<?php
namespace AppHttpControllersCms;
use AppHttpControllersController;
use AppHttpRequestsUserRequest;
use AppServicesCmsUsersService;
use IlluminateHttpRequest;
class UsersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
public function store(UserRequest $request)
{
$users_service = new CmsUsersService($request->all(), null, null);
$result = $users_service->create();
return redirect()->back()->with('message', $result);
}
/**
* Display the specified resource.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function update(UserRequest $request, $id)
{
$users_service = new CmsUsersService($request->all(), $id, null);
$result = $users_service->update();
return redirect()->back()->with('message', $result);
}
/**
* Remove the specified resource from storage.
*
* @param  string  $users_id
* @return IlluminateHttpResponse
*/
public function destroy($users_id)
{
$users_service = new CmsUsersService(null, null, $users_id);
$result = $users_service->delete();
return redirect()->back()->with('message', $result);
}
}

你可以给参数一个默认值,像这样:

public function __construct(?array $data = [], ?int $user_id = null, ?string $users_to_be_deleted = null)
{
$this->user_id = $user_id;
$this->data = $data;
$this->users_to_be_deleted = $users_to_be_deleted;
}

现在你可以这样写:

$users_service = new CmsUsersService();

见:https://3v4l.org/InkBY

最新更新