2014-01-08 41 views
0

Iam試圖創建如果可能在WordPress或通過只是PHP的是我有如下功能。用於調用其他函數的主要php函數

function myfunction_displays_something_one() { 

echo "string"; 
echo "string"; } 

function myfunction_displays_something_two() { 

echo "string"; 
echo "string"; 

} 

function myfunction_displays_something_three($arg1, $arg2) { 

echo "<p>"; 
echo $arg1 $arg2; 
echo "/<p>"; 

} 

我想上述功能通過

function myfunction_displays($display) { 

    if ($display == 'something-one') 
    return myfunction_displays_something_two(); 

    if ($display == 'soemthing-two') 
    return myfunction_displays_something_two(); 

} 

被調用,顯示當叫奧鋼聯php文件想叫三大功能上面,像這樣

<?php myfunction_displays(something-two); ?> 
<?php myfunction_displays(something-three); ?> 

我已經能夠做出前兩項工作,因爲他們沒有任何爭論,但我無法調用第三個函數,因爲它有爭論。

有沒有辦法來創建這可能使用WordPress的過濾器或只是PHP?

+0

,您可以撥打[類](http://us3.php.net/manual/en/language.oop5.basic.php) –

+0

當然,但你爲什麼想要做到這一點? – Steve

+0

感謝您的快速回復,有沒有可以分享的例子,以便我可以指導自己? – user3174600

回答

0

在線演示:https://eval.in/87273

function myfunction_displays($display ,$arrArg = array()) { 

     if ($display == 'something-one') 
     return myfunction_displays_something_two(); 

     if ($display == 'soemthing-two') 
     return myfunction_displays_something_two(); 

     if ($display == 'soemthing-three') 
     { 
      $a1 = isset($arrArg[0]) ? $arrArg[0] : ""; 
      $a2 = isset($arrArg[1]) ? $arrArg[1] : ""; 
      return myfunction_displays_something_three($a1,$a2); 
     } 

    } 

,並調用它是這樣的:

<?php myfunction_displays('something-two'); ?> 
<?php myfunction_displays('something-three',array(0=>'value1',1=>'value2')); ?> 
+0

爲什麼你是很快 ??無論如何謝謝 – hizbul25

+0

@ user3174600:你試過這個嗎? –

+0

@AwladLiton謝謝它累了,它輸出了我需要的。有沒有辦法在不使用if語句和isset的情況下在WordPress中獲得相同的結果。 – user3174600

0

下面是一個簡單的方法:

function myfunction_displays($display, $arg1=NULL, $arg2=NULL) { 

    if ($display == 'something-one') 
    return myfunction_displays_something_two(); 

    if ($display == 'something-two') 
    return myfunction_displays_something_two(); 

    if ($display == 'something-three') 
    return myfunction_displays_something_three($arg1, $arg2); 

} 

的 「= NULL」 部分是指你不每次打電話時都不必填寫。

一個例子:

<?php myfunction_displays('something-two'); ?> 
<?php myfunction_displays('something-three', 'value1', 'value2'); ?> 
+0

不會使用call_user_func()或call_user_func_array()會更清潔和更簡單嗎? –

+0

@Mark Ba​​ker call_user_func()對我來說很難理解,因爲我是初學者,所以我沒有提供它。 – miyasudokoro

相關問題