如果 Class 存在 && 如果方法存在 PHP / Laravel
If Class exists && If Method exists PHP / Laravel
PHP/Laravel
嘿,我正在 php 中进入抽象,并尝试根据已提交的内容验证和存储值,我希望这些方法既不知道要验证什么 and/or class 和这样做的方法 -
我得到的有效,但我可以看到 classes/methods 不存在的问题。这是我的问题。
如果我要调用以下格式的方法,哪种方式最好 'check' if class_exists()
或 method exists()
?
public function store(Request $request)
{
$dataSet = $request->all();
$inputs = $this->findTemplate();
$errors = [];
$inputValidators = [];
foreach ($inputs as $input) {
$attributes = json_decode($input->attributes);
if (isset($attributes->validate)) {
$inputValidators[$input->name] = $input->name;
}
}
foreach ($dataSet as $dataKey => $data) {
if (array_key_exists($dataKey, $inputValidators)) {
$validate = "validate" . ucfirst($dataKey);
$validated = $this->caseValidator::{$validate}($data);
if ($validated == true) {
$inputValidators[$dataKey] = $data;
} else {
$errors[$dataKey] = $data;
}
} else {
$inputValidators[$dataKey] = $data;
}
}
if (empty($errors)) {
$this->mapCase($dataSet);
} else {
return redirect()->back()->with(['errors' => $errors]);
}
}
public function mapCase($dataSet)
{
foreach($dataSet as $dataKey => $data) {
$model = 'case' . ucfirst($dataKey);
$method = 'new' . ucfirst($dataKey);
$attribute = $this->{$model}::{$method}($dataKey);
if($attribute == false) {
return redirect()->back()->with(['issue' => 'error msg here']);
}
}
return redirect()->back->with(['success' => 'success msg here'])'
}
对于一些额外的上下文,输入表单将包含一组输入,这可以随时更改。因此,我将所有值存储为 json 'payload'。
当用户首先提交所述表单时,会找到活动模板,其中提供了应验证内容的详细信息$input->attributes
,一旦定义好,我就可以从 caseValidator
模型中调用函数作为 $this->caseValidator::{$validate}($data);
.
我认为这里不需要任何检查是否存在,因为验证参数是针对输入定义的,因此如果 none 存在,将使用 if (array_key_exists($dataKey, $inputValidators))
[=26 跳过此检查=]
但是,我使用 mapCase()
将一些数据分散到第二个代码块中的其他表中。这实际上是在遍历所有数组键,而不管它的方法是否存在,因此无法进行初始检查,如第一个块中所示。我曾尝试使用 class_exists()
和 method_exists
,但从逻辑上讲它不适合,我不能指望它们按我的意愿工作,也许我在 mapCase
中的方法不正确?我想如果我为每个键定义一个 class 我应该使用一个 class 并且那里有方法,这将消除检查现有 class 的需要。请指教
参考:
$attribute = $this->{$model}::{$method}($dataKey);
使用 class_exists()
解决了潜在问题,考虑到我知道方法名称,因为它们与 $dataKey
.
相同
public function mapCase($dataSet)
{
foreach($dataSet as $dataKey => $data) {
$model = 'case' . ucfirst($dataKey);
if (class_exists("App\Models\CaseRepository\" . $model)) {
$method = 'new' . ucfirst($dataKey);
$attribute = $this->{$model}::{$method}($dataKey);
}
if($attribute == false) {
return redirect()->back()->with(['issue' => 'error msg here']);
}
}
return redirect()->back->with(['success' => 'success msg here'])'
}
PHP/Laravel
嘿,我正在 php 中进入抽象,并尝试根据已提交的内容验证和存储值,我希望这些方法既不知道要验证什么 and/or class 和这样做的方法 -
我得到的有效,但我可以看到 classes/methods 不存在的问题。这是我的问题。
如果我要调用以下格式的方法,哪种方式最好 'check' if class_exists()
或 method exists()
?
public function store(Request $request)
{
$dataSet = $request->all();
$inputs = $this->findTemplate();
$errors = [];
$inputValidators = [];
foreach ($inputs as $input) {
$attributes = json_decode($input->attributes);
if (isset($attributes->validate)) {
$inputValidators[$input->name] = $input->name;
}
}
foreach ($dataSet as $dataKey => $data) {
if (array_key_exists($dataKey, $inputValidators)) {
$validate = "validate" . ucfirst($dataKey);
$validated = $this->caseValidator::{$validate}($data);
if ($validated == true) {
$inputValidators[$dataKey] = $data;
} else {
$errors[$dataKey] = $data;
}
} else {
$inputValidators[$dataKey] = $data;
}
}
if (empty($errors)) {
$this->mapCase($dataSet);
} else {
return redirect()->back()->with(['errors' => $errors]);
}
}
public function mapCase($dataSet)
{
foreach($dataSet as $dataKey => $data) {
$model = 'case' . ucfirst($dataKey);
$method = 'new' . ucfirst($dataKey);
$attribute = $this->{$model}::{$method}($dataKey);
if($attribute == false) {
return redirect()->back()->with(['issue' => 'error msg here']);
}
}
return redirect()->back->with(['success' => 'success msg here'])'
}
对于一些额外的上下文,输入表单将包含一组输入,这可以随时更改。因此,我将所有值存储为 json 'payload'。
当用户首先提交所述表单时,会找到活动模板,其中提供了应验证内容的详细信息$input->attributes
,一旦定义好,我就可以从 caseValidator
模型中调用函数作为 $this->caseValidator::{$validate}($data);
.
我认为这里不需要任何检查是否存在,因为验证参数是针对输入定义的,因此如果 none 存在,将使用 if (array_key_exists($dataKey, $inputValidators))
[=26 跳过此检查=]
但是,我使用 mapCase()
将一些数据分散到第二个代码块中的其他表中。这实际上是在遍历所有数组键,而不管它的方法是否存在,因此无法进行初始检查,如第一个块中所示。我曾尝试使用 class_exists()
和 method_exists
,但从逻辑上讲它不适合,我不能指望它们按我的意愿工作,也许我在 mapCase
中的方法不正确?我想如果我为每个键定义一个 class 我应该使用一个 class 并且那里有方法,这将消除检查现有 class 的需要。请指教
参考:
$attribute = $this->{$model}::{$method}($dataKey);
使用 class_exists()
解决了潜在问题,考虑到我知道方法名称,因为它们与 $dataKey
.
public function mapCase($dataSet)
{
foreach($dataSet as $dataKey => $data) {
$model = 'case' . ucfirst($dataKey);
if (class_exists("App\Models\CaseRepository\" . $model)) {
$method = 'new' . ucfirst($dataKey);
$attribute = $this->{$model}::{$method}($dataKey);
}
if($attribute == false) {
return redirect()->back()->with(['issue' => 'error msg here']);
}
}
return redirect()->back->with(['success' => 'success msg here'])'
}