Php-mysql Course


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 a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
Click on the "echo" correct instruction.
echo "" echo ""; echo """;
echo "Address URL:";
OOP - Constants, Static Properties and Methods

Last accessed pages

  1. Get Mime Type of file or string content in PHP (6148)
  2. Disable button and Enable it after specified time (17416)
  3. Working with HTML attributes in PHP (13578)
  4. Redirects (4874)
  5. Get Duration of Audio /Video file before Upload (15355)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (270)
  2. Read Excel file data in PHP - PhpExcelReader (63)
  3. The Mastery of Love (59)
  4. PHP Unzipper - Extract Zip, Rar Archives (58)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (42)