Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Static Methods and Properties

Henrique Zafniksi
Henrique Zafniksi
8,560 Points

Scope Resolution Operator (paamayim nekudotayim), in object context?

Hampton Paulk demonstrated how to obtain the static property value by using

echo $obj::$manufacturer;

Is this correct? Dreamweaver warns for Syntax Error.

I always read and thought you could only use those scope operators within class context, like:

class A { static $prop = 1; }

echo A::$prop;

not

$obj = new A; echo $obj::$prop;

PHP manual clearly states you cannot use static property in object context.

http://php.net/manual/en/language.oop5.static.php

"A property declared as static cannot be accessed with an instantiated class object"

2 Answers

Hi Henrique,

When you do $obj::$prop you're not accessing the static property through the object. The scope resolution operator is referring to the class that the object was instantiated from. So you're still accessing the static property through the class.

Each instantiation of the class does not get it's own copy of $prop, but rather $prop belongs to the class and all objects of the class share it.

Consider the following code example:

<?php 
class A {
    public static $prop = 1;
    public $prop2 = 2;
    public static function getStaticProp() {
        return self::$prop;
    }
}
$obj = new A();
echo A::$prop . '<br>'; //Outputs 1
echo $obj::$prop . '<br>'; // Outputs 1
echo $obj->prop . '<br>'; // Issues Notice about undefined property
echo $obj->getStaticProp() . '<br>'; // Outputs 1
echo $obj->prop2 . '<br>'; // Outputs 2
?>

Notice that $prop2 can be accessed through the object but $prop can't because it's static. I believe it's that 3rd echo that the php manual is saying you can't do.

From http://php.net/manual/en/language.oop5.static.php

A property declared as static cannot be accessed with an instantiated class object (though a static method can).

I think it further clarifies that a few paragraphs down:

Static properties cannot be accessed through the object using the arrow operator ->.

Looking at the 4th echo you can see that a static method can be accessed through the object. The static property is still retrieved through the class using the scope resolution operator and the self keyword since it's within the class.

Henrique Zafniksi
Henrique Zafniksi
8,560 Points

Very explanatory and useful answer, thank you!

Thanks Andrew.

You're welcome Henrique.

Aaron Munoz
Aaron Munoz
11,177 Points

May you blessed with many children and have a lineage of a thousand generations.

Andrew Shook
Andrew Shook
31,709 Points

Henrique, yes what Hampton did is correct. I'm not sure why dreamweaver is giving you a syntax error (I haven't used DW in more than a decade). Also, I'm not sure why the PHP manual says that you can not access a static property on an instantiated class. It is possible, so I don't know what's going here. Maybe, Hampton Paulk could clear this up a bit.