search for in the  
<Static KeywordObject Abstraction>
Last updated: Thu, 19 May 2005

Object Constants

It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don't use the $ symbol to declare or use them. Like static members, constant values can not be accessed from an instance of the object.

Example 19-15. Defining and using a constant

<?php
class MyClass
{
   const
constant = 'constant value';

   function
showConstant() {
       echo 
self::constant . "\n";
   }
}

echo
MyClass::constant . "\n";

$class = new MyClass();
$class->showConstant();
// echo $class::constant;  is not allowed
?>


User Contributed Notes
Object Constants
esad at 25novembar dot com
25-Apr-2005 04:03
Refering to caliban at darklock dot com's article:

The whole idea of visibility is implementing the concept of data hiding and encapsulation. This means exposing as little as possible of the class variables/methods, in order to maintain loose coupling. If you reference all your variables in your class directly, you've probably missed the point of OOP.

If the variable visibility is set to private it shouldn't be readable outside the class (performing tricks to read it is pointless, if you want to read something, make it public, it's your code). This is not used to obfuscate/hide a variable from someone but to enforce good coding practice of maintaining the loose coupling between objects.

http://c2.com/cgi/wiki?CouplingAndCohesion
tobias_demuth at web dot de
24-Apr-2005 01:39
Please note, that it is not possible to initialize an object's constant with a not-constant value. Something like this won't work:

<?php

class Testing {
     const
TIME = time();

    
// Some useful code
}

echo
Testing::TIME;

?>

It will break with: Parse error: syntax error, unexpected '(', expecting ',' or ';' in path/to/file on line anylinenumber

Hope this will help somebody...
Rasqual
28-Mar-2005 09:18
'''constant values can not be accessed from an instance of the object'''
What the docs mean are that, as shown in the example,
<?php
$instance
= new Class();
echo
$instance::const_name;
?>
is not allowed.
From a method,
<?php
echo self::const_name;
?>
is perfectly allowed.
I think the sentence should be rephrased to:
'constant values can not be accessed (through/by the means of) an instance of the class'
douglass_davis at earthlink dot net
03-Feb-2005 07:34
Re: caliban at darklock dot com

most people are not going to do all of this:

<?php
  
if(isset($y["@$classname@$b"]))
       echo
"\"$b\" is private: {$y["@$classname@$b"]}<br/>";
?>

to read an object variable.

My point is: what you said is true, however access specifiers do have an effect on who gets to read the variables when you are not trying to bypass encapsulation:

<?php

class Foo
{
   private
$varname=2;
}

$obj=new Foo();
echo
$obj->varname// accessing in the usual way doesn't work
?>

So: const gives you a constant that is public in terms of reading them the usual way.  A private const would mean you could not read the variable using the 2nd method above.  Not to say it's an "omission in PHP," but, realize that there would be some value added in allowing consts to be made private.
pcarmody at c-spanarchives dot org
18-Jan-2005 09:54
It should be noted that you cannot use the return from a function to assign a value to a class constant:

<?php
class MyClass {
  const
good = "blah";
  const
bad = str("blah", 0, 3);  // causes a parse error
}
?>
caliban at darklock dot com
15-Dec-2004 01:55
Lest anyone think this is somehow an omission in PHP, there is simply no point to having a protected or private constant. Access specifiers identify who has the right to *change* members, not who has the right to read them:

<?php
// define a test class
class Test
{
   public static
$open=2;
   protected static
$var=1;
   private static
$secret=3;
}

$classname="Test";

// reflect class information
$x=new ReflectionClass($classname);
$y=array();
foreach(
$x->GetStaticProperties() as $k=>$v)
  
$y[str_replace(chr(0),"@",$k)]=$v;

// define the variables to search for
$a=array("open","var","secret","nothing");
foreach(
$a as $b)
{
   if(isset(
$y["$b"]))
       echo
"\"$b\" is public: {$y["$b"]}<br/>";
   elseif(isset(
$y["@*@$b"]))
       echo
"\"$b\" is protected: {$y["@*@$b"]}<br/>";
   elseif(isset(
$y["@$classname@$b"]))
       echo
"\"$b\" is private: {$y["@$classname@$b"]}<br/>";
   else
       echo
"\"$b\" is not a static member of $classname<br/>";
}
?>

As you can see from the results of this code, the protected and private static members of Test are still visible if you know where to look. The protection and privacy are applicable only on writing, not reading -- and since nobody can write to a constant at all, assigning an access specifier to it is just redundant.
pezskwerl at gmail dot com
08-Dec-2004 12:02
Unlike static members, constants always have public visibility, so trying to set a constant's visibility won't work, such as in this example:

<?php
class MyClass {

     protected static
$nonConstant = "this will work";
     protected const
constant = "this won't work";
}
?>

<Static KeywordObject Abstraction>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 18:35:34 2005 EDT