2017-12-09 15 views
0

私が持っているのnこのようなPHPオブジェクト:PHPオブジェクト内にどのように条件を追加できますか?

 $test 
     ->key1(1) 
     ->key2(1) 
     ->key3(1); 

は、その中に条件を追加することが可能ですか?このような

:あなたが欲しいものを

$test 
    if(true) ->key1(1) 
    ->key2(1) 
    if(true) ->key3(1); 
+1

($ test - > key1(1) - > key2(1) - > key3(1))何かを行うとどうしてですか? – FatalError

+0

https://stackoverflow.com/questions/10046629/conditional-builder-method-chaining-fluent-interfaceおよびhttps://stackoverflow.com/questions/1189979/implementing-conditional-in-a-を参照してください。流暢なインターフェイス。 – Progman

+0

この状態で何をチェックしますか? – shudder

答えて

0

を例

if ($YOUR_KEY == "key1") { 
    //if key1 then do something 
} elseif ($YOUR_KEY == "key2") { 
    //if key2 then do something 
} else { 
    //if none condition work than do something else 
} 
ここ $YOUR_KEY

あなたの変数があると"key1"ため

if (condition) { 
    code to be executed if this condition is true; 
} elseif (condition) { 
    code to be executed if this condition is true; 
} else { 
    code to be executed if all conditions are false; 
} 

では、あなたの価値ある

チェックwww.w3schools.comhttp://php.net/manual参照用

+0

switch文がはるかに良くなり、きれいです;) – Stony

+0

はい良い点。 – Regolith

関連する問題