此代碼的工作,可能有更好的方法來實現的事情,但我的問題是,傳遞函數作爲參數傳遞給另一個函數
有沒有經過任何這樣的特定術語?(與匿名函數),
這是一個可以接受的做法? ,
是針對標準嗎?
<?php
// suppose the only way i can retrieve the content is by using this function
//like wordpress equivalent of the_content()
function mycontent()
{
$Original_content="oldcontent";
return $Original_content;
}
//instead of ---> echo $Original_content."facebook,twitter code";
//if i define a new function
function social_share($content)
{
$added_social=$content;
$added_social.=" + facebook,twitter code...";
echo $added_social;
}
//function call
social_share(mycontent());
?>
在此先感謝:)
你有某種非常基本的...控制器也許,但它是相當最小。從那裏工作,並建立它。看看它是否有效,然後可能作爲獎勵檢查,如果它擴大。 – stefgosselin
你正在做什麼是完全正常的,但你實際上並沒有經過「一個函數作爲參數傳遞給另一個函數」。 social_share(mycontent())首先評估mycontent(),該函數的結果被傳遞給社交共享,而不是函數mycontent。但這只是術語,代碼本身很好。 我不認爲你在做什麼有一個特殊的術語。 它只是簡寫:$ tmp = mycontent(); social_share($ TMP);你的代碼是可取的,因爲你沒有創建無用的變量。 – rx80