Rajesh Paul
Rajesh Paul

Reputation: 7019

static variable not accessible via an object or $this but static functions are accessible via $this or object in PHP

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

Upvotes: 2

Views: 1256

Answers (3)

hakre
hakre

Reputation: 197775

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

Upvotes: 0

Emacs The Viking
Emacs The Viking

Reputation: 199

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! :)

Upvotes: 2

rid
rid

Reputation: 63462

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.

Upvotes: 7

Related Questions