1

Rather than having multiple lines of the same code on each method, I want to set up a (Boot/initialization) method to define common values and such. Then, call that method when needed.

Initially, I had something like this:

<?php
class boot {
    private static $root = $_SERVER['DOCUMENT_ROOT'] . '/';


    public static function load() {
        return self::$root;
    }
}

$test = boot::load();
echo $test;
?>

But I would get an error saying something like: Parse error: syntax error, unexpected T_VARIABLE in...

So, I changed it to:

<?php /* Class Bootstrap (Boot for short) */
class boot {
    private static $base = null;
    private static $root = null;


    private static function boot(){
        self::$base = $_SERVER['DOCUMENT_ROOT'] . '/';
    }


    public static function load() {
        self::boot();
        return self::$base;
    }
}

$test = boot::load();
echo $test;
?>

Then I got this: Fatal error: Constructor boot::boot() cannot be static in...

So I resorted to:

<?php
class boot {
    private static $base = null;
    private static $root = null;


    private function boot(){
        $this->base = $_SERVER['DOCUMENT_ROOT'] . '/';
    }


    public static function load() {
        $this->boot();
        return self::$base;
    }
}

$test = boot::load();
echo $test;
?>

But I am still geting an error, Fatal error: Using $this when not in object context in... I tried different things but I am out of ideas.

Omar
  • 11,783
  • 21
  • 84
  • 114
  • `self::$base` and `self::ini()` – Mark Baker Jan 17 '14 at 00:08
  • @mark-baker That did not work. could you try it, in an answer? – Omar Jan 17 '14 at 00:21
  • [Read the documentation](http://php.net/oop/). – Sverri M. Olsen Jan 17 '14 at 00:30
  • I really don't get it. If you are using an all-static class, simply initialize static variables once. If you're planning on instanciating your class, put the initializations where they belong, i.e. in the constructor. Or am I missing something? – kuroi neko Jan 17 '14 at 00:32
  • @kuroineko 黒い猫, $base is used several times. I am also calling functions like boot:function() . Static functions cannot call a construct. Also, in this example you only see one line, but I have more. adding the same line of code over and over is not productive – Omar Jan 17 '14 at 01:30
  • 僕の名前を書いてくれてありがとう ;) Could you give us a more explicit example of your current (working) code? What you describe does not seem unusual to me, so I don't see why you would want a specific mechanism. – kuroi neko Jan 17 '14 at 01:49

2 Answers2

3

You cannot use $ this in a static context.

When to use self over $this?

To sum up the above answer you have to use $this To access the members that belong to the current Object (non-static) but use self :: to access the class's static members.

You could make all your functions static and use self :: instead of $this-> .

Community
  • 1
  • 1
Patrick Geyer
  • 1,515
  • 13
  • 30
1

Try the following

class boot {
    private static $base = null;
    private static $root = null;

    public static function load() {
        self::$base = $_SERVER['DOCUMENT_ROOT'] . '/';
        return self::$base;
    }
}

$test = boot::load();
echo $test;
xelber
  • 4,197
  • 3
  • 25
  • 33
  • That sure works,. What I fail to see is the point in jumping through all these hoops . – kuroi neko Jan 17 '14 at 00:34
  • @kuroi, true, I don't see the point either.. just wanted to show the difference in Class variables and instance variables. Omar has to read on that more I am sure. – xelber Jan 17 '14 at 00:36
  • Yes, I think he would better :) – kuroi neko Jan 17 '14 at 01:01
  • multiply load() twenty times over, with different functions. It is not what I am looking for. I want to initialize some properties in one blow. not on every function (x 20). Again, this is just one line, but I got lots more – Omar Jan 17 '14 at 01:32