1

I have the following class, and for some reason it's not accessing the test property. Why is this? I'm new to OOP, so please be easy on me. Thanks

class Test {
    private $test;

    function __contruct() {
        $this->test = "test";
    }

    static function test() {
        echo $this->test;
    }
}

$test = new Test;
$test::test();
5
  • 3
    By those edits that you've made (removing the static keyword on the test() method and the associated changes) the concept differed completely, It's now a no problem question !!! Commented May 3, 2015 at 6:40
  • It's still not echoing anything Commented May 3, 2015 at 6:41
  • 1
    I thought it's a typo, but you need to change the __contruct() to __construct(). Commented May 3, 2015 at 6:43
  • 1
    By the way, I'd say that it was generally bad form to edit the question in such a way that the answers now make no sense. Commented May 3, 2015 at 6:50
  • 1
    @TheGeneral I agree, and mightyspaj3, you should edit your question and return the static keyword back to its place (before the function test() tokens). Commented May 3, 2015 at 6:52

3 Answers 3

3

Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.

PHP Documentations.

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

4 Comments

What if it was a public function? That still doesn't work either
It is independent of visibility, also take a look at this.
I don't understand, I just want to know why I can't echo $this->this
If you instantiate a class, you create an object. the $this refers to object; now, static methods are available without the existence of even one object, so, simply you cannot access $this in static methods :)
0

Good morning. It seems you have 3 issues with your code.

  1. There is a syntax error at constructor line change it from __contruct to __construct.
  2. Change test function visibility to public
  3. Access your function with the -> instead of ::

1 Comment

The second item, the visibility issue!, has nothing to do with the concept of static methods!
0

To elaborate further on the above answers: Static methods and variables are not linked to any particular instance of the object, this is why you have to call test with $test::test(). This also means that you cannot access an instance variable from without a static method and it doesn't really make sense to do so (If you had multiple instances of the object with different values set for that variable, how would the interpreter know which instance/value to use?)

If you want to have a field accessible from a static method then you have to make the field static as well. So, if you wanted to have $test accessible from your static method test() then you'd have to write your function as something along these lines:

class Test {
    private static $test;

    function __contruct() {
        Test::$test = "test";
    }

    public function test() {
        echo Test::$test;
    }
}

$test = new Test;
$test::test();

However, it doesn't really make sense to be initialising a static field like that in your constructor. So you'd more likely be wanting to do something like:

class Test {
    private static $test = "test";

    function __contruct() {
    }

    public static function test() {
        echo Test::$test;
    }
}

$test = new Test;
$test::test();

Or, if you don't actually require test() to be static then you could just make it an instance method:

class Test {
    private $test = "test";

    function __contruct() {
        $this->$test = "test"
    }

    public function test() {
        echo $this->$test;
    }
}

$test = new Test;
$test->test();

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.