Aaron Saray

open source programmer,
web developer

entrepreneur, author
and musician

My Blog

contains PHP, Web and business/entrepreneurial related content. Please join in the conversation!

To clear up that confusion about public and public static access in PHP

Apparently, a few programmers I know have been getting confused about access of public, public static variables in PHP classes. I’ve written this example code with the following comments to explain what will work – and what won’t.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
class TEST
{
    /** public variable **/
    public $pubvar = 'pubvar';

    /** static public variable **/
    public static $pubvarstatic = 'pubvarstatic';

    public function __construct()
    {
        /** this works **/
        var_dump($this->pubvar);

        /** this also works **/
        var_dump(self::$pubvarstatic);

        /** this would not - no error **/
        var_dump($this->pubvarstatic);
    }

    public static function statictest()
    {
        /** this works - static accessing a static **/
        var_dump(self::$pubvarstatic);

        /**
         * this does not work - static has no business accessing a non static class var
         * generates error:
         * Fatal error:  Access to undeclared static property:  TEST::$pubvar
         */

        var_dump(self::$pubvar);
    }
}
print "<pre>";

/** for testing __construct() **/
new TEST;

/** this works - public static access **/
var_dump(TEST::$pubvarstatic);

/** for testing static method statictest() **/
TEST::statictest();

This entry was posted in PHP and tagged . Bookmark the permalink.

3 Responses to To clear up that confusion about public and public static access in PHP

  1. William says:

    ‘Ello–

    Really, it’s just the definition of a Static item that people seem to be confused about. a Static item, be it an object, method, variable is simply that they are accessible statically without dependence on initiating other objects.

    I recommend, after reading this blog entry, that you also read http://us.php.net/global — go down to Static Variables.

    -Will

  2. John Reeves says:

    Good point. Also, this:
    var_dump($this->pubvarstatic);

    gives no error because it works. I found it will only say anything if you have E_STRICT enabled. In that case, $this->pubvarstatic is null, but you could assign something to it. just like any other attribute in a class, you can create attributes on the fly in php. So you could do:

    $this->pubvarstatic = 9;
    echo $this->pubvarstatic; // echoes 9
    echo self::$pubvarstatic; // still echoes ‘pubvarstatic’

    I mean, you can but you shouldn’t.

    Also, it would probably be polite if people strictly declare methods as static if they are to be used that way. PHP doesn’t care if you use a method statically or not regardless of how it was declared. Even with E_NOTICE and E_STRICT enabled (unless you access $this).

    So, you could have:
    static function foo(){…}
    ….
    $x = new TEST();
    $x->foo(); // works.
    TEST::foo(); // works.

    And PHP won’t care. I don’t know. Just thought I’d point that out. Use static functions statically, instance functions non-statically.

  3. Sven Svensson says:

    Thank you VERY much for this! Saved me a lot of future frustration! Made everything clear!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>