2016-04-13 6 views
-1

Ich möchte diese Funktion kurz. Ich habe auch einen kleinen Teil davon veröffentlicht, aber es ist jedes Mal das gleiche Prinzip:Shortcut ähnliche Befehle in PHP-Funktion

if(in_array($infinitiveVerb, 
    IrregularExceptionGroup::$name_in_lowercase)) { 
     $exceptionmodel = ExceptionModel::NAME_IN_UPPERCASE; 
} 

PHP-Funktion

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'; 
} 

Wie Ist das möglich?

+0

was genau ist die Frage? Ich habe keine Ahnung, was du fragst. – DevDonkey

+0

Sie wissen, dass Sie die Variable in jeder if-Anweisung überschreiben, wenn sie eingegeben wird? Sind Sie sicher, dass Sie nicht möchten, wenn/elseif? – Rizier123

+0

Ich möchte nicht ähnliche Bedingungen haben, weil sie sehr ähnlich sind. Wie man eine if-Bedingung für alle 'ExceptionModels' verwendet. – Grischa

Antwort

1

Das einzige, was ich sehen kann, und würde hier ändern, ist nur jedes irregularExceptionGroup in ein Array gestellt, wie folgt aus:

function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 

    $exceptionmodel = ExceptionModel::NO_EXCEPTIONS; 

    $irregularExceptionGroupArray = [ 
      ExceptionModel::ALLER => IrregularExceptionGroup::$aller, 
      ExceptionModel::AVOIR_IRR => IrregularExceptionGroup::$avoir_irr, 
      ExceptionModel::ETRE_IRR => IrregularExceptionGroup::$etre_irr, 
     ]; 

    foreach($irregularExceptionGroupArray as $exceptionModel => $irregularExceptionGroup){ 
     if(in_array($infinitiveVerb, $irregularExceptionGroup)){ 
      $exceptionmodel = $exceptionModel; 
      //break; //If you don't want to overwrite the variable, just uncomment this 
     } 
    } 

    return new ExceptionModel($exceptionmodel); 
} 
0

Sie alle Ausnahmen in einer Lookup kombinieren, statt dieser in_array prüft alle die Zeit. (Hinweis: Wenn vom Kurs der InfinitVerb eine __toString irgendeiner Art hat)

Zum Beispiel IrregularExceptionGroup::$aller enthält: ['aller', 'allez', 'je suis', 'paris'] und IrregularExceptionGroup::$avoir_irr enthält ['some', 'more', 'stuff']

Ändern Sie es an:

IrregularExceptionGroup::$allExceptions = [ 
    'aller' => ExceptionModel::ALLER, 
    'allez' => ExceptionModel::ALLER, 
    'je suis' => ExceptionModel::ALLER, 
    'paris' => ExceptionModel::ALLER, 
    'some' => ExceptionModel::AVOIR_IRR, 
    'more' => ExceptionModel::AVOIR_IRR, 
    'stuff' => ExceptionModel::AVOIR_IRR 
]; 


function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 
    $ex = ExceptionModel::NO_EXCEPTIONS; 
    if (array_key_exists($infinitiveVerb, IrregularExceptionGroup::$allExceptions)) { 
     $ex = IrregularExceptionGroup::$allExceptions[$infinitiveVerb]; 
    } 

    return new ExceptionModel($ex); 
} 

Und man konnte es sogar machen "kürzer" durch den ternären Operator:

function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 
    return new ExceptionModel(isset(IrregularExceptionGroup::$allExceptions[$infinitiveVerb]) ? IrregularExceptionGroup::$allExceptions[$infinitiveVerb] : ExceptionModel::NO_EXCEPTIONS); 
} 

oder PHP 7:

function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 
    return new ExceptionModel(IrregularExceptionGroup::$allExceptions[$infinitiveVerb] ?? ExceptionModel::NO_EXCEPTIONS); 
}