473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 23296
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Joshua Beall | last post by:
Hi All, I read in a forum somewhere that namespaces, though once supposed to be a part of PHP5, have been removed. Is this true? Can anyone show me the official statement by Zend that they...
17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
1
by: Arne Claus | last post by:
Hi. I've got a strange problem here with PHP5 (5.0.4). I've got file A, defining a class A and a file B, which sets up an Object of A (aObj) in the global scope like this. ----- A.php ----...
5
by: Victor Hannak | last post by:
I have a class that needs to reference a const array in several of its methods. Where should I put the declaration/initialization of this array so that it is only created once when the class is...
3
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
5
by: Paul Smitton | last post by:
Hello, I would like to be able to store some constant data that is specific to each descendant class. This data would then be accessable by base class functions. However, I cannot find out how...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.