470,855 Members | 1,227 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

PHP5: const and class scope problems


Say I have a class with a constant in it. I also have a variable in
that class that I would like to set to that constant as the
initialization value. Why doesn't the following work?

class Square
{
const DEFAULT_SIZE=5;
var $size=self::DEFAULT_SIZE; // how do i set $size to DEFAULT_SIZE?
}

$r= new Square();

Errors are:

PHP Fatal error: Cannot access self:: when no class scope is active in
C:\src\test\consttest.php on line 9

Fatal error: Cannot access self:: when no class scope is active in
C:\src\test\consttest.php on line 9

Which is wrong, because a class scope is active!! I think it is trying
to inline the code, and it is doing it incorrectly.

-d
Jul 17 '05 #1
4 23146
Doug wrote:
Say I have a class with a constant in it. I also have a variable in
that class that I would like to set to that constant as the
initialization value. Why doesn't the following work?

class Square
{
const DEFAULT_SIZE=5;
var $size=self::DEFAULT_SIZE; // how do i set $size to DEFAULT_SIZE?
}

$r= new Square();


Bad OOP. :)

Try this:

class Square {

const DEFAULT_SIZE = 5;
public $size; // 'var' is deprecated in PHP5

public function __construct() {
$this->size = $this->DEFAULT_SIZE;
}

}

$r = new Square();
Jul 17 '05 #2


Berislav Lopac wrote:
Bad OOP. :)
On the contrary, In I believe this is great OOP. :) Even if it is bad
OOP, it still shouldn't cause a compile-time error. Constant
initializers are allowed for any variable.

Example:

class Square {
public $size=5;
}

$r = new Square();

works just fine. As does this example given in the PHP5 manual:

class SimpleClass
{
// member declaration
public $var = 'a default value';

// method declaration
public function displayVar() {
echo $this->var;
}
}

From the manual: In PHP 4, only constant initializers for var variables
are allowed.

And that goes for PHP 5 too. This even works:

class Constants
{
const DEFAULT_SIZE=5;
}

class Square
{
var $size=Constants::DEFAULT_SIZE;
}

The ONLY difference is that when you try to use a constant from the
class' own definition. In that case, it can not find the constant,
saying no class scope is active.

-d

Try this:

class Square {

const DEFAULT_SIZE = 5;
public $size; // 'var' is deprecated in PHP5

public function __construct() {
$this->size = $this->DEFAULT_SIZE;
}

}

$r = new Square();

Jul 17 '05 #3
Doug wrote:
The ONLY difference is that when you try to use a constant from the
class' own definition. In that case, it can not find the constant,
saying no class scope is active.


Maybe because the class is not completely loaded at that point.
Jul 17 '05 #4
Zurab Davitiani wrote:
Maybe because the class is not completely loaded at that point.


Indeed, because it is an interpreted language, PHP wouldn't create a
scope for the class until AFTER the class has bee interpreted. Remember
that rule about only being able to reference stuff AFTER it's declared?
well, you can only reference the class AFTER it's been declared, which
means the constant too, as it is part of the class.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Joshua Beall | last post: by
17 posts views Thread by Terje Slettebø | last post: by
1 post views Thread by Arne Claus | last post: by
5 posts views Thread by Victor Hannak | last post: by
3 posts views Thread by Virendra Verma | last post: by
5 posts views Thread by Paul Smitton | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.