Besides properties and methods in the body of the class can also be defined constants.
A constant is declared with the const keyword followed by a name (is not prefixed with a dollar sign like properties).
  - Syntax:
The main difference between properties and constants is that the constant value cannot be changed once it is set.
Constants are recognized as public, they are not defined with public, private or protected attribute.
Constants are accessed via the class name in conjunction with   ::
  - Syntax:
To access a class constant in a PHP script it's no need to create an object instance of the class.

To distinguish more clearly the properties by constants, they are often named using only uppercase characters.

  - Example:
The Test class below contains a constant (PI), and a method (getArea)
<?php// Test class
class Test {
  // aset a constant
  const PI = 3.14;

  public function getArea($raza) {
    if (is_numeric($raza)) {
      return Test::PI * pow($raza, 2);
    else exit('Incorrect value for $raza');
The getArea() method returns the area of the circle with the radius of $raza (mathematical formula is:   PI * radius2 ).
To test this class and how to access the constant, use the following script.
// include Test class

echo Test::PI;        // display the PI constant value

// create an object instance
$objTest = new Test();

// display area of the circle of radius 78, returned getArea()
echo '<br /> Area = '. $objTest->getArea(78);
- Notice how the PI constant is accessed within the class body, and outside class in the script, before to create an object instance of the class.
The example above will display:
Area = 19103.76

Attempting to set a value on a constant once it has been declared will cause a parse error.
- Inside the class, the constants can also be accessed with the self keyword in place of the class name:

Static Properties and Methods

The static Properties and Methods can be accesed through the class name rather then objects.
Static properties and static methods are declared by using the static keyword before the "public", "private" or "protected" attribute.
  - Syntax:
static attribute $property = value;

static attribute function method_Name()
- attribute is one of the keywords: public, private or protected, which define the allowed level of access.

The static Properties and Methods can be accesed without needing an object instance of the class. They use the class name in conjunction with   ::
  - Syntax:
Notice that the static property use the dollar sign ($).
Static properties cannot be accessed through the object using the arrow operator "->"
Static methods can only use static properties, becouse the pseudo-variable $this is not available inside the method declared as static.

• To access a static method or property from within the same class you can also use the self keyword. "self" is to classes what the $this pseudo-variable is to objects.

  - Example:
In the next class, called ElStatic, we declare two static properties, the constructor and a static method.
// elStatic class
class elStatic {
  // declare static properties
  static private $site = '';           // Private
  static public $id = 78;                       // Public

  // Constructor
  public function __construct($site) {
    /* if the $site argument is string type, with more than 3 characters,
      assigns its value to $site property and calls getProp() method
      else, display a message and ends the script
    if(is_string($site) && strlen($site)>3) {
      elStatic::$site = $site;
      self::getProp();             // calls using the "self" keyword
    else exit('Invalid value for $site');

  // Metoda statica
  static public function getProp() {
    // outputs a string with the values of the static properties
    echo '<br /> ID = '. elStatic::$id. ' - site: '. self::$site;
The __construct() method receives one argument and use it to change the value of the "$site" static property, then calls the getProp() method.
The __construct() method is used only when an object instance is created.
The static method, getProp() return a string including the values of the two static properties (one is accessed by using the class name, for the other is used the "self" keyword).
To test the class above and see how the static elements work, use the following script.
// include the elStatic classs

echo elStatic::$id;        // output the value of the $id static property (78)
elStatic::getProp();       // calls the getProp()  static method

// change the value of the $id static property
elStatic::$id = 89;

// create an object instance of the elStatic classs
$obj = new elStatic('');
• Notice that the value of static property that is public (here, "id") can be changed outside the class and influences all instructions in which is used (as it would be modified in the class body), including all object instances of the class, created after the property value was modified.
The example above will output:
ID = 78 - site:
ID = 89 - site:
- By creating the instance it's obtained the result given by the constructor method.

• In a class can be used all these elements: general properties and methods, constants, static methods and properties, but in the static methods body can be used only constants and static elements.

Daily Test with Code Example

Which tag create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong></strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert(""); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
OOP - Constants, Static Properties and Methods

Last accessed pages

  1. AnythingZoomer - Zoom part of Image or Text Content (1137)
  2. Flash Games - Free online Games (13354)
  3. Butterfly Kyodai (25370)
  4. Blosics 2 (612)
  5. Raft Wars (609)

Popular pages this month

  1. Bubbles3 (3680)
  2. Butterfly Kyodai (3290)
  3. Zuma Deluxe (2408)
  4. Qwop (1695)
  5. Backgammon (1414)