Home > PHP, PHP Tutorials, PHP5 OOPS Tutorials > PHP5 Tutorial – Defining Class Constants

PHP5 Tutorial – Defining Class Constants

November 15th, 2007 admin Leave a comment Go to comments

In PHP4 the only constants that we would declare were global constants. In PHP5 it is possible to define a class level constant. These constants are specific to the class and hence don’t clutter the global level constant space.





To declare a constant in a class, PHP5 provides developers with a new keyword i.e. const; look at the example below:

class Customer {
	const TYPES = "Anything";
}
 
echo "Types are : " . Customer::TYPES;

In the above example, const is a keyword and TYPES is the name of the constant. Outside the class definition we echo the value of the constant by using the scope resolution operator (::) like this Customer::TYPES. Observe that we don’t need to create an object of the class to make use of the constant.

The next logical question is if we can create an object of the Customer class and using the scope resolution operator access the constant. The answer is no; reason – because a constant belongs to the class definition scope and not to an object.
Example of accessing Constants within a function

class Customer {
	const TYPES = "Anything";
 
	public function showConstant() {
		echo "Echo from showConstant() : " . Customer::TYPES;
	}
 
}
 
$c = new Customer();
$c->showConstant();

Output:
Echo from showConstant() : Anything

Some observations on Constants

  1. Variables defined as constants cannot be changed.
  2. Only a string or numeric value can be assigned to a constant.
  3. Arrays, Objects & Expressions cannot be assigned to a constant.
  4. A class constant can only be accessed via the scope resolution operator (::) executed on the class name.
  5. A class constant cannot have <a>access specifiers</a> assigned to it (private, public & protected)

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – $this variable explained
  9. PHP5 Tutorial – instanceOf Operator Explained
  10. PHP5 Tutorial – Magic Methods – __toString() method
  11. PHP5 Tutorial – Magic Methods – __get() and __set()
  12. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  13. PHP5 Tutorial – Magic Methods – __call() method
  14. PHP5 Tutorial – Magic Methods – __autoload() method
  15. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  16. PHP5 Tutorial – Magic Methods – __clone() method




Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Stephan
    November 17th, 2007 at 19:30 | #1

    Generally speaking, the scope resolution operator (::) can access static properties and static methods, constants and overridden methods of a class

    Btw, in point 5. (A class constant cannot have access specifiers assigned to it (private, public & protected)), there is some markup that should – I assume – not be there.

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )