PHP OOP - Constants, Static Properties and Methods

Constants

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:
const CONSTANT_NAME
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:
Class_Name::CONSTANT_NAME
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.
<?php
// 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:
3.14
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:
                    self::CONSTANT__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:
ClassName::$property
ClassName::method_Name()
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.
self::$property
self::method_Name()

  - Example:
In the next class, called ElStatic, we declare two static properties, the constructor and a static method.
<?php
// elStatic class
class elStatic {
  // declare static properties
  static private $site = 'http://coursesweb.net';           // 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.
<?php
// 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('www.marplo.net');
?>
• 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:
78
ID = 78 - site: http://coursesweb.net
ID = 89 - site: www.marplo.net
- 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.