2013-09-21 34 views
0

我試圖讓所選語言出現在功能buildMenu()的鏈接中。使用具有屬性的靜態類php

我想使用它作爲一個靜態函數,所以我可以在我的頭模板中調用它。如果我在init()函數中調用該函數,那麼它可以正常工作,但是,當我嘗試將它用作靜態函數時,什麼都不起作用。我已經嘗試了我所知道的一切,所以看起來我對PHP的知識就此結束:)

你們有沒有得到任何提示?提前致謝!

class bootstrap { 

    static public $lang; 
    static public $class; 
    static public $method; 

    public function init(){ 
     $url = isset($_GET['url']) ? $_GET['url'] : null; 
     $url = rtrim($url, '/'); 
     $url = filter_var($url, FILTER_SANITIZE_URL); 
     $url = explode('/', $url); 

     //Set values on startup 
     if($url[0] == NULL) {$url[0] = 'nl';} 
     if($url[1] == NULL) {$url[1] = 'index';} 

     if(isset($url[0])) {$this->lang = $url[0];} 
     if(isset($url[1])) {$this->class = $url[1];} 
     if(isset($url[2])) {$this->method = $url[2];}   

     $this->loadClass(); 

    } 

    public function loadClass(){ 

     $filename = 'libs/' . $this->class . '.php'; 
     if(file_exists($filename)){ 
      $newController = new $this->class($this->lang, $this->class, $this->method); 
      $newView = new view($this->lang, $this->class, $this->method); 
     } else { 
      $newclass = new error($this->lang, $this->class, $this->method); 
     } 

    } 


    public function buildMenu(){ 
     echo '<li><a href="http://localhost/testing/' . $this->lang . '/foto">Foto</a></li>'; 
    } 

    /* 
    * Set paths 
    */ 

    public static function root(){ 
     echo "http://localhost/testing/"; 
    } 

} 
+0

'自:: $ LANG,自:: $類,...' – elclanrs

回答

1

您正在使用對象操作者->)代替範圍解析運算符::),其被用於引用類的常量和靜態屬性或方法。

請參閱here以獲取靜態關鍵字的解釋和靜態屬性的使用。

更新您的代碼如下:

class bootstrap{ 

    static public $lang; 
    static public $class; 
    static public $method; 

    public function init(){ 
    $url = isset($_GET['url']) ? $_GET['url'] : null; 
    $url = rtrim($url, '/'); 
    $url = filter_var($url, FILTER_SANITIZE_URL); 
    $url = explode('/', $url); 

    //Set values on startup 
    if($url[0] == NULL) {$url[0] = 'nl';} 
    if($url[1] == NULL) {$url[1] = 'index';} 

    if(isset($url[0])) {self::$lang = $url[0];} 
    if(isset($url[1])) {self::$class = $url[1];} 
    if(isset($url[2])) {self::$method = $url[2];}  

    $this->loadClass(); 

    } 

    public function loadClass(){ 

    $filename = 'libs/' . self::$class . '.php'; 
    if(file_exists($filename)){ 
     $newController = new self::$class(self::$lang, self::$class, self::$method); 
     $newView = new view(self::$lang, self::$class, self::$method); 
    } else { 
     $newclass = new error(self::$lang, self::$class, self::$method); 
    } 

    } 


    public static function buildMenu(){ 
     echo '<li><a href="http://localhost/testing/' . self::$lang . '/foto">Foto</a></li>'; 
    } 

    public static function root(){ 
    echo "http://localhost/testing/"; 
    } 
} 
+0

不給它一個錯誤上的「$ newController行=新self :: lang(self :: $ lang,self :: $ class,self :: $ method);「 ? – Axll

+0

公平點,錯過了$ – GordyD

+0

作品像一個魅力,thx!而thx的對象操作符和範圍解析操作符之間的信息:) – Axll

0

正如@elclanrs已經提到的,如何改變buildMenu()方法

public static function buildMenu(){ 
    echo '<li><a href="http://localhost/testing/' . self::$lang . '/foto">Foto</a></li>'; 
} 

可以使用bootstrap::buildMenu()然後調用它。