Axll
Axll

Reputation: 55

using a static class with properties in php

I'm trying to get the selected language to appear in a link with the function buildMenu().

I would like to use it as a static function so I can call it in my header template. If I call the function in the init() function it all works fine, however, when I try to use it as a static function, nothing works anymore. I've tried everything I know, so it seems my knowledge of php ends there :)

Any of you got any tips? Thanks in advance!

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/";
    }

}

Upvotes: 0

Views: 175

Answers (2)

GordyD
GordyD

Reputation: 5103

You are using the object operator (->) instead of the scope resolution operator (::) that is used to reference class constants and static properties or methods.

See here for an explanation of the static keyword and working with static properties.

Update your code to this:

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/";
  }
}

Upvotes: 1

George Brighton
George Brighton

Reputation: 5151

As @elclanrs has already mentioned, how about changing the buildMenu() method to

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

You can then call it using bootstrap::buildMenu().

Upvotes: 0

Related Questions