Get a static property of an instance

14,498

Solution 1

You need to lookup the class name first:

$class = get_class($thing);
$class::$property

$property must be defined as static and public of course.

Solution 2

From inside a class instance you can simply use self::...

class Person {
  public static $name = 'Joe';
  public function iam() {
    echo 'My name is ' . self::$name;
  }
}

$me = new Person();
$me->iam(); // displays "My name is Joe"

Solution 3

If you'd rather not

$class = get_class($instance);
$var = $class::$staticvar;

because you find its two lines too long, you have other options available:

1. Write a getter

<?php
class C {
  static $staticvar = "STATIC";
  function getTheStaticVar() {
    return self::$staticvar;
  }
}
$instance = new C();
echo $instance->getTheStaticVar();

Simple and elegant, but you'd have to write a getter for every static variable you're accessing.

2. Write a universal static-getter

<?php
class C {
  static $staticvar = "STATIC";
  function getStatic($staticname) {
    return self::$$staticname;
  }
}
$instance = new C();
echo $instance->getStatic('staticvar');

This will let you access any static, though it's still a bit long-winded.

3. Write a magic method

class C {
  static $staticvar = "STATIC";
  function __get($staticname) {
    return self::$$staticname;
  }
}
$instance = new C();
echo $instance->staticvar;

This one allows you instanced access to any static variable as if it were a local variable of the object, but it may be considered an unholy abomination.

Solution 4

classname::property;

I think that's it.

Solution 5

You access them using the double colon (or the T_PAAMAYIM_NEKUDOTAYIM token if you prefer)

class X {
    public static $var = 13;
}
echo X::$var;

Variable variables are supported here, too:

$class = 'X';
echo $class::$var;
Share:
14,498

Related videos on Youtube

commonpike
Author by

commonpike

Updated on June 17, 2020

Comments

  • commonpike
    commonpike almost 4 years

    If I have an instance in PHP, what's the easiest way to get to a static property ('class variable') of that instance ?

    This

    $classvars=get_class_vars(get_class($thing));
    $property=$classvars['property'];
    

    Sound really overdone. I would expect

    $thing::property
    

    or

    $thing->property
    

    EDIT: this is an old question. There are more obvious ways to do this in newer PHP, search below.

  • commonpike
    commonpike about 13 years
    Confirmed, that works , php 5.3.0> ... still a bit overdone, but i guess this is the shortest. thanks !
  • commonpike
    commonpike about 13 years
    No, "As of PHP 5.3.0, it's possible to reference the class using a variable".
  • commonpike
    commonpike about 13 years
    A static property is a property of the class, and hence meaningful information for all instances .. in all languages except php :-)
  • Shakti Singh
    Shakti Singh about 13 years
    @pike: That's true but they are not bounded with any specific instance
  • Sinus the Tentacular
    Sinus the Tentacular over 6 years
    Oh, and remember to use static::$$staticname if you extend this class and want to use the subclass' statics, not the parent's.

Related