我想簡化這個功能。我還發表它的一小部分,但它每次都相同的原則:在PHP函數中縮短了類似的命令
if(in_array($infinitiveVerb,
IrregularExceptionGroup::$name_in_lowercase)) {
$exceptionmodel = ExceptionModel::NAME_IN_UPPERCASE;
}
PHP函數
function finding_exception_model(InfinitiveVerb $infinitiveVerb)
{
$exceptionmodel = ExceptionModel::NO_EXCEPTIONS;
if (in_array($infinitiveVerb, IrregularExceptionGroup::$aller)) {
$exceptionmodel = ExceptionModel::ALLER;
}
if (in_array($infinitiveVerb, IrregularExceptionGroup::$avoir_irr)) {
$exceptionmodel = ExceptionModel::AVOIR_IRR;
}
if (in_array($infinitiveVerb, IrregularExceptionGroup::$etre_irr)) {
$exceptionmodel = ExceptionModel::ETRE_IRR;
}
return new ExceptionModel($exceptionmodel);
}
ExceptionModel.php
class ExceptionModel extends Enum
{
const NO_EXCEPTIONS = 'no exceptions';
const ALLER = 'aller';
const AVOIR_IRR = 'avoir_irr';
const ETRE_IRR = 'etre_irr';
}
如何這可能嗎?
究竟是什麼問題?我不知道你在問什麼。 – DevDonkey
你知道你在每個if語句中覆蓋變量,如果它進入它?你確定你不想要if/elseif嗎? – Rizier123
我不想有類似條件,因爲它們非常相似。如何爲所有'ExceptionModel'使用條件。 – Grischa