# Bug with static property access

- Thread: https://externals.io/message/55696
- Original archive: https://news-web.php.net/php.internals/55696
- Started by Chris Stockton on 2011-10-03T20:26:04+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #55696 — Chris Stockton — 2011-10-03T20:26:04+00:00

URL: https://externals.io/message/55696#55696

Hello,

I noticed the following odd behavior earlier today, there is
definitely a bug here in my opinion, the bug is either the error
message or the behavior. I think the behavior is possibly expected.
I'll let some others comment.

The reason I think the error is odd is because it is very misleading
in a much larger code base (what I was debugging) you go to the line
it is complaining about and are perplexed because you are not
attempting to access the mentioned constant. I'm sure the engine does
some kind of lazy/runtime determination that causes this, that area
may need a look at?

Example:
<?php
abstract class ClassA {
  static protected $_cache = Array();

  public $version = self::VERSION;

  final static public function MethodOne() {
    return __METHOD__;
  }

  final static public function MethodTwo() {
    self::$_cache;
    return __METHOD__;
  }
}

abstract class ClassB extends ClassA {
  const VERSION = 1;
}

var_dump(ClassB::MethodOne());
var_dump(ClassB::MethodTwo());

?>

// prints
string(17) "ClassA::MethodOne"
Fatal error: Undefined class constant 'self::VERSION' in
<SNIP>/testbug.php on line 14

---

## #55699 — Matthew Weier O'Phinney — 2011-10-04T18:33:35+00:00

URL: https://externals.io/message/55696#55699 · In reply to: #55696

> Hello,
>
> I noticed the following odd behavior earlier today, there is
> definitely a bug here in my opinion, the bug is either the error
> message or the behavior. I think the behavior is possibly expected.
> I'll let some others comment.
>
> The reason I think the error is odd is because it is very misleading
> in a much larger code base (what I was debugging) you go to the line
> it is complaining about and are perplexed because you are not
> attempting to access the mentioned constant. I'm sure the engine does
> some kind of lazy/runtime determination that causes this, that area
> may need a look at?
>
> Example:
><?php
> abstract class ClassA {
>   static protected $_cache = Array();
>
>   public $version = self::VERSION;
>
>   final static public function MethodOne() {
>     return __METHOD__;
>   }
>
>   final static public function MethodTwo() {
>     self::$_cache;
>     return __METHOD__;
>   }
> }
>
> abstract class ClassB extends ClassA {
>   const VERSION = 1;
> }
>
> var_dump(ClassB::MethodOne());
> var_dump(ClassB::MethodTwo());
>
> ?>
>
> // prints
> string(17) "ClassA::MethodOne"
> Fatal error: Undefined class constant 'self::VERSION' in
><SNIP>/testbug.php on line 14

That makes complete sense to me -- ClassA is referring to self, which
resolves to ClassA... which does not define a "VERSION" constant. Change
to this:

    public $version = static::VERSION;

and it should be fine.

-- 
Matthew Weier O'Phinney
Project Lead            | matthew@zend.com
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

---

## #55700 — Chris Stockton — 2011-10-04T21:36:22+00:00

URL: https://externals.io/message/55696#55700 · In reply to: #55699

Hello,

On Tue, Oct 4, 2011 at 11:33 AM, Matthew Weier O'Phinney
<weierophinney@php.net> wrote:
>
> That makes complete sense to me -- ClassA is referring to self, which
> resolves to ClassA... which does not define a "VERSION" constant. Change
> to this:
>
>    public $version = static::VERSION;
>
> and it should be fine.
>

Hi Matt, I knew what was wrong with the code as soon as I saw it :- ),
the code itself was part of a much more complicated system and I was
using it in a unexpected way. The reason it was difficult to
troubleshoot is because the constant being whined about was not any
(of the several) constants being evaluated at the line the error was
for. I think the error should be thrown at compile time, not at
runtime ONLY when a constant is being accessed. This could cause
dangerous hard to catch errors that make it into production.

In addition, as I understand and would like to make you aware that
static is not allowed in compile time class constants. Which is
slightly unusual and perhaps could be changed because it seems that
there is already runtime resolving taking place here.

---

## #55701 — Christian Kaps — 2011-10-05T06:28:31+00:00

URL: https://externals.io/message/55696#55701 · In reply to: #55700

> > […]
> In addition, as I understand and would like to make you aware that
> static is not allowed in compile time class constants. Which is
> slightly unusual and perhaps could be changed because it seems that
> there is already runtime resolving taking place here.
> 

It's available since PHP 5.3. This feature is called "late static binding".

---

## #55702 — Chris Stockton — 2011-10-05T06:56:15+00:00

URL: https://externals.io/message/55696#55702 · In reply to: #55701

Hello,

On Tue, Oct 4, 2011 at 11:28 PM, Christian Kaps
<christian.kaps@mohiva.com> wrote:
>
>
> It's available since PHP 5.3. This feature is called "late static binding".
>

Before anyone else responds to my post, please read the entire message!

It's simple really, the error message is odd and misleading for the
example code I gave.

In addition I pointed out later in the thread that LSB is not
supported for class constants, something that could perhaps be added,
although it is likely disabled as a (good) design decision, I could
see use cases where it would be useful.

Thanks,

-Chris

---

## #55703 — Dan Birken — 2011-10-05T18:18:01+00:00

URL: https://externals.io/message/55696#55703 · In reply to: #55702

This does seem strange.  To me the error message looks fine, but it is weird
that "MethodOne" works while "MethodTwo" doesn't.  Seems like both should
fail in a perfect world.  However, this seems like a situation where bad
code is tickling some weird corner case in PHP, not a situation where PHP is
doing anything wrong.

-Dan

On Tue, Oct 4, 2011 at 11:56 PM, Chris Stockton
<chrisstocktonaz@gmail.com>wrote:

> Hello,
>
> On Tue, Oct 4, 2011 at 11:28 PM, Christian Kaps
> <christian.kaps@mohiva.com> wrote:
> > […]
> binding".
> > […]
>
> Before anyone else responds to my post, please read the entire message!
>
> It's simple really, the error message is odd and misleading for the
> example code I gave.
>
> In addition I pointed out later in the thread that LSB is not
> supported for class constants, something that could perhaps be added,
> although it is likely disabled as a (good) design decision, I could
> see use cases where it would be useful.
>
> Thanks,
>
> -Chris
