0

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

}
1
  • self::$lang, self::$class, ... Commented Sep 21, 2013 at 22:10

2 Answers 2

1

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/";
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't it give an error on the line of "$newController = new self::lang(self::$lang, self::$class, self::$method);" ?
Works like a charm, thx! And thx for the info between the object operator and scope resolution operator :)
0

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.