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.

6
  • self::$base and self::ini() Commented Jan 17, 2014 at 0:08
  • @mark-baker That did not work. could you try it, in an answer? Commented Jan 17, 2014 at 0:21
  • Read the documentation. Commented Jan 17, 2014 at 0: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? Commented Jan 17, 2014 at 0: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 Commented Jan 17, 2014 at 1:30

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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;

1 Comment

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

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.