Php-mysql Course

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 = 'https://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('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: https://coursesweb.net
ID = 89 - site: 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.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
OOP - Constants, Static Properties and Methods

Last accessed pages

  1. JavaScript Course - Free lessons (31647)
  2. Volume and Surface Area Calculator for 3D objects (11276)
  3. Using v-model in form input fields (1051)
  4. jQuery UI draggable - Drag elements (11445)
  5. Display data from PHP Array, or MySQL in HTML table (26980)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (310)
  2. The Mastery of Love (48)
  3. CSS cursor property - Custom Cursors (36)
  4. Read Excel file data in PHP - PhpExcelReader (35)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (31)