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
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
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.
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".
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
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.comwrote:
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