2014-02-28 27 views
0
<?php 
class config { 
    public static function get($path = null) { 
     if ($path) { 
      $config = $GLOBALS['config']; 
      $path = explode('/', $path); 

      foreach($path as $bit) { 
       if (isset($config[$bit])) { 
        $config = $config[$bit]; 
       } 
      } 

      return $config; 
     } 
     return false; 
    } 
} 

?> 

我跟隨本教程http://www.youtube.com/watch?v=S6vDgLwJ7n8&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc來自phpacademy。我迷路了,不能理解這條線如何閱讀複雜變量?

function get ($path = null) ----這是什麼意思?根據我的理解,它說「得到$path的價值」。但是這引出了另一個問題,我在哪裏可以得到$path的價值?請啓發我,並用英文翻譯此聲明。

if ($path) -----檢查$ path是否有值嗎?

$config = $config[$bit] ----這是我第一次遇到這個,我無法理解,因爲有一個括號中有一個變量。請賜教,教我如何翻譯這本書,並用純英文閱讀。

+0

數組教程:http://oreilly.com/catalog/progphp/chapter/ch05.html –

+0

參考:[php中的函數](http://www.php.net/manual/en/language.functions。 php) – Gordon

+0

在附註中,您可能想要找到更好的教程,因爲您顯示的代碼存在一些問題,例如它訪問全局狀態和靜態,這很容易導致不可維護的應用程序充滿副作用和僵化,很難更改代碼。 – Gordon

回答

2
function get($path = null) 

該行聲明瞭一個名爲get功能將接受一個名爲path一個參數。它還爲path提供了一個默認值,該值爲空。簡單地說,如果沒有路徑發送到此函數,則認爲路徑爲null值。

這可以通過不同的示例

function display($message="Hello World") 
    { 
    echo $message; 
    } 

將更好地解釋如果該函數調用等

display("Testing"); // It will output `Testing` 
display(); // It will output `Hello World` 

其次

$config[$bit]; 

$config陣列,它的數值有一個索引存儲在$bit變量中。

例如

$a=array(); 
$a["test"]=1; 
$index="test"; 

echo $a["test"]; //echoes 1 
echo $a[$index]; //echoes 1 
+0

感謝您的解釋! – zlloyd

0
function get($path = null) 

這是爲$path的默認值已經設置函數聲明。沒有更多或更少。默認值是NULL這意味着......好吧,沒什麼。您仍然可以交出參數並覆蓋默認值。