2
<?php
class c1
{
  public static function f1()
  {
    return "hello";
  }

  public static $a=10;

  public function f2()
  {
    echo $this->f1(); //prints "hello"
    echo $this->a;//ERROR:Undefined property: c1::$a in C:\wamp\www\class_in_php\example5.php on line 14
  }
}

$obj1=new c1;
$obj1->f2();
?>

Why can't we access a static variable of a class using $this or an object of that class??? But we can access a static function of that class using $this or an object of that class.

What is the reason behind such a phenomenon?

2
  • If you are looking for logical explanations for all inconsistencies in PHP, you are wasting your time. Commented Sep 8, 2013 at 8:27
  • @rid: there's no warning for this case. In fact, other languages (e.g. C++) allow calling static functions this way as well. "[A static function] does not need to be invoked through an object of its class, although for convenience, it may." Commented Sep 8, 2013 at 8:32

3 Answers 3

7

You should use self:: instead of $this-> to access static members.

The reason is that $this refers to the current instance of the class, while static members are part of the class itself, not of the instance.

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

4 Comments

Then why the static function is accessible through $this, they should also belong to the class only.
@RajeshPaul, neither should be callable through $this, but for some reason, PHP allows it. They even have a paragraph in the docs about it: "Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can)." They offer no more explanation.
thanx for understanding my actual concern.
You could use self:: but you can use $this:: as well, just for some explanation that was missing so far: stackoverflow.com/a/24059368/367456
2

A static variable belongs not to an "instance" but to the class itself. When you have in actual "instance" of the class at runtime, then and only then does the $this pointer make sense: it means "this instance that I find myself inside right now"... how could you use the $this pointer to reference something that doesn't exist outside of an instance?

When I first learned C++ it was with (Metacomco I think) a system that actually used a huge pile of C preprocessor macros to simulate objects and it was very enlightening to see and hence understand that the $this (this in C++) is in fact just an extra parameter passed as the first parameter to all method functions:

this->foo("Hello");
this->bar(42, "Finished");

is actually executed like this:

foo(this_ptr, "Hello");
bar(this_ptr, 42, "Finished");

and inside the foo() function any reference to a method variable such as:

this->status

is nothing more than a reference to a pointer dereferenced variable:

this_ptr->status

So you can see that trying to access a static variable from a this pointer is going to blow because it just isn't a member of that particular chunk of memory. That's how things "used to work" but I think the explanation is still a good one.

Hope that help! :)

3 Comments

A very nice explanation but imho a little incomplete, can you please advise them how to then access the static property then?
I didn't see the point in doing that because it had already been explained well enough in the previous answers and comments. I just gave some subjective background based on experience that's all.
But my question was- why the static methods are accessible through $this or an object of that class when it is not allowed with the static variables to be accessed that way. As in case of java both of them can be accessed in terms of an object. Moreover the restriction is such that no non-static member of a class can be accessed by that ClassName but not that a static member of a class cannot be accessed in terms of an object of that class. I think u get my actual question.
0

Why can't we access a static variable of a class using $this or an object of that class? But we can access a static function of that class using $this or an object of that class.

Well, we can, however you used the wrong syntax.

Wrong:

echo $this->a;

Right:

$this::$a;

As c1::$a is a static class variable, you need to use the right syntax, that is with double-colon :: and then the dollar-sign ($) to denote the variable: $this::$a.

However, do not get fooled by that syntax too easy, because the reason that

$this->f1()

works while c1::f1() is a static function is because of backwards compatibility as before PHP version 5 there were no static class methods (as those explicitly defined by the static keyword) and with the very first PHP 5 version -> could be used to call static class methods.

However to access static class variables via $this is a PHP 5.3+ syntax feature, so much newer.

Example code (run against multiple PHP versions):

<?php
/**
 * @link http://stackoverflow.com/a/24059368/367456
 */
class c1
{
    public static function f1()
    {
        return "hello";
    }

    public static $a = 10;

    public function f2()
    {
        echo $this->f1(); // prints "hello"
        echo $this->a;    // Strict Standards: Accessing static property c1::$a as non static
        echo $this::$a;   // prints "10" (PHP <= 5.2.17: Parse error)
    }
}

$obj1 = new c1;
$obj1->f2();

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.