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 adds an image in web page?
<div> <img> <span>
<img src="" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
OOP - Constants, Static Properties and Methods

Last accessed pages

  1. AJAX with POST and PHP (7837)
  2. Qwop (8221)
  3. Register and show online users and visitors (14103)
  4. Ajax-PHP Chat Script (27384)
  5. JavaScript code and PHP (13755)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (49383)
  2. Read Excel file data in PHP - PhpExcelReader (37738)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (36753)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33179)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (30548)