[英]What is the best way to reuse code in PHP?
if ( $_GET['tab'] == 'newest' ) {
// Go through each question
foreach( array_reverse( $end_array, true ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
{
// Grab the title for the first array
$title = $titles [ $tags_and_Qid['question_id'] ] ['title'];
// Grab the tags for the question from the second array
$tags = $end_array [ $tags_and_Qid['question_id'] ] ['tag'];
// Grab the username for the question from the second array
$username = $usernames [ $tags_and_Qid['question_id'] ] ['username'];
--- cut ----
}
}
我需要經常使用此代碼。 唯一的區別是第一個示例中的array_reverse (..., true)
。
我試圖通過使函數organize_question
解決此問題來解決該問題。 我沒有成功:
function organize_questions ( $tab ) {
if ( $_GET['tab'] == 'newest' ) {
echo ( "array_reverse ( $end_array , true )" );
// Problem here!
}
if ( $_GET['tab'] == 'oldest' ) {
echo ( "$end_array" );
// this does not work
} else {
echo ( "array_reverse ( $end_array , true )" );
// Problem here!
}
}
然后,我將代碼中的相關行更改為此:
foreach( organize_question( $tab ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
問題在於將變量從一個函數傳遞到另一個函數。
我試圖將所有必要的變量都放在函數的參數中,但是由於此函數有很多依賴關系,所以一切都壞了。
我剛接觸PHP,因此必須有比我嘗試的方法更簡單的方法。
聽起來這部分代碼完成了大部分工作:
// Go through each question
foreach( array_reverse( $end_array, true ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
{
-- cut ---
}
我將從您的organize_questions()
函數中分離出$_GET['tab']
檢查,並在其他地方進行參數確定。 像這樣:
function organize_questions($array)
{
foreach($array as $questionId => $title )
{
//do the work
}
}
然后將決策代碼置於其他位置:
if ( $_GET['tab'] == 'newest' )
{
organize_questions(array_reverse ( $end_array , true ));
}
else if ( $_GET['tab'] == 'oldest' )
{
organize_questions($end_array);
}
else
{
//etc.
}
function organize_questions ()
{
if ( $_GET['tab'] == 'newest' )
{
print_r( array_reverse( $end_array , true ) );
}
else if ( $_GET['tab'] == 'oldest' )
{
print_r($end_array);
}
else
{
print_r(array_reverse ( $end_array , true ) );
}
}
我刪除了回聲並使用了print_r(假設那些變量實際上是數組)。 另外,除非您在函數中的其他位置使用$ tab,否則不需要它。
編輯:我實際上不會使用print_r ...它對於調試等很有用。 通常,您需要某種方法從實際要顯示的數組中選取碎片,並對每個碎片使用echo或print。
EDIT2:我對此同時表示贊成和反對。 它使用正確的語法重寫了相關功能。 問題的某些部分含糊不清,因此我將繼續。 您似乎還要求將信息傳遞給函數。 有問題的$ _GET ['tab']正在訪問get變量(yoursite.com/index.php?tab=newest)。 您似乎要問的是根本如何使用函數。 您正確使用了:
function organize_questions( $tab )
{
...
}
假設您要使用變量標簽。 為了使用此函數,您可以從文件中的另一個函數或執行php_require或php_include的另一個文件中這樣調用它:
$mytab = 'bob';
organize_questions( $mytab);
然后,您將在先前創建的函數中使用原始的$ tab,或者使用參數列表中的$ tab進行上述操作
這是一個好問題。 您絕對可以花很長時間嘗試各種不同的方法,以防止自己重用代碼。 我可能會執行上面列出的功能建議之一,但另一種選擇是將代碼放在單獨的PHP文件中,然后將其包含在所需的位置。 這基本上等同於其他語言中的內聯函數,並且如果您擔心執行速度,這是一個不錯的方法。 但是,在大多數情況下,您將更加擔心要通過http發送客戶端的頁面大小,因此這不像編寫函數那樣令人滿意。 我主要是指出每種情況都有不同的“最佳”解決方案-在您的情況下,我會說McAden的答案是一個很好的答案。
使用包括:
//myscript.php
if ( $_GET['tab'] == 'newest' )
{
print_r( array_reverse( $end_array , true ) );
}
else if ( $_GET['tab'] == 'oldest' )
{
print_r($end_array);
}
else
{
print_r(array_reverse ( $end_array , true ) );
}
然后在您的代碼中:
//myexecutionplace.php
$end_array = foo;
include 'myscript.php';
doStuffWith($end_array);
$end_array = foo2;
include 'myscript.php';
doStuffWith($end_array2);
您正在尋找的是一種策略...。
$strategies = array(
'oldest' => create_function(
'$questions',
'return organize_questions($questions);'
),
'hottest' => create_function(
'$questions',
'return organize_questions(sort_by_hottness($questions));'
),
'default' => create_function(
'$questions',
'return organize_questions(array_reverse($questions, true));'
),
);
$strategy = 'default';
if (array_key_exists($strategies, $_GET['tab'])
$strategy = $_GET['tab'];
print_r( $strategies[$strategy]($questions) );
基本上,您說的是要處理(排序)這些事情(問題)。
您可能還想看看usort函數, http: //www.php.net/manual/en/function.usort.php
function sortArray($direction, $array)
{
switch ($direction) {
case 'oldest':
return array_reverse($array, true);
case 'newest':
return $array;
default:
return array();
}
}
function processQuestions($array)
{
foreach($array as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] ) {
//code
}
}
$sortedArray = sortArray($tab, $end_array);
processQuestions($sortedArray);
您可能應該重寫以下內容。
foreach($array as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
//could be rewritten as
foreach($array as $question_id => $title)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.