各ページの異なるバージョンに異なるアカウント タイプが表示されるサイトを作成しています。私はそれを機能させていますが、私の質問は速度/「ベストプラクティス」に関するものです。これを行うための最良の方法は、次のうちどれ (またはまったく異なるもの) ですか?
オプション 1. 各アカウント タイプをファイルの個々のセクションに分割します。
if($accountType == "type1"){
//All lines of code for the account type1 page
}
elseif($accountType == "type2"){
//All lines of code for the account type2 page
}
elseif($accountType == "type3"){
//All lines of code for the account type3 page
}
オプション 2. インクルード ファイルを使用して、各アカウント タイプをファイルのセクションに分割します。
if($accountType == "type1"){
//this file has all of the code for account type1 page
require('includes/accounts/type1/file.php');
}
elseif($accountType == "type2"){
//this file has all of the code for account type1 page
require('includes/accounts/type2/file.php');
}
elseif($accountType == "type3"){
//this file has all of the code for account type1 page
require('includes/accounts/type3/file.php');
}
オプション 3. ファイル全体で多くの条件ステートメントを使用して、アカウントの種類ごとにページを生成します。
if($accountType == "type1"){
$result = mysql_query("//sql statement for account type1");
}
elseif($accountType == "type2"){
$reslut = mysql_query("//sql statement for account type2");
}
elseif($accountType == "type3"){
$result = mysql_query("//sql statement for account type3");
}
while ($row = mysql_fetch_array($result)) {
$variable1 = $row['variable1'];
if($accountType == "type1"){
$variable2 = $row['type1Variable2'];
$variable3 = $row['type1Variable3'];
}
elseif($accountType == "type2"){
$variable2 = $row['type2Variable2'];
}
elseif($accountType == "type3"){
$variable2 = $row['type3Variable2'];
}
$variable4 = $row['Variable4'];
}
echo "The variables echoed out are $variable1, $variable2";
if($accountType == "type1"){
echo ", $variable3";
}
echo "and $variable4";
//the rest of the file to follow in the same way
基本的には次のようになります。
オプション 1: ファイルは 1000 行のコードです。
オプション 2: ファイルは 30 行のコードで、各インクルード ファイルは 250 ~ 350 行のコードです。オプション 3: ファイルは 650 行のコードです。一部のコードは 3 つのアカウント タイプすべてで「共有」できるため、それは少なくなります。
どのオプションが最速/「ベストプラクティス」になりますか? 全体的なファイル サイズが小さくなるため、オプション 3 に傾いていますが、このオプションにはさらに多くの条件ステートメントがあります (オプション 1 と 2 には 3 つの条件ステートメントしかありませんが、オプション 3 にはたとえば 40 があります)。これだけ多くの条件ステートメントを使用すると、ファイルの処理が遅くなりますか? オプション 1 とオプション 2 の間に実際に違いはありますか (コードのブロックをインクルード ファイルに分割することは、アカウントの種類ごとに 1 つのインクルード ファイルのみを読み込むことを意味しますか? それとも、php は 3 つのファイルすべてを読み込み、適切なファイルを選択するだけですか?)
ご協力いただきありがとうございます!