Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP5 Tutorial - Defining Class Constants

Posted on : 15-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

1

If you're new here, you may want to subscribe to my Newsletter. Thanks for visiting!

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




Comments (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.

Write a comment

Enter this code