const CONSTANT_NAMEThe main difference between properties and constants is that the constant value cannot be changed once it is set.
Class_Name::CONSTANT_NAMETo 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.
<?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 ).
<?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.
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 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.
ClassName::$property ClassName::method_Name()Notice that the static property use the dollar sign ($).
self::$property self::method_Name()
<?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.
<?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.
<input type="checkbox" name="a_name" value="value" checked="checked" />
#id { background:url("path_to_image.png"); background-size:contain; background-repeat:no-repeat; }
var rest8_7 = 8 % 7; alert(rest8_7);
$nr = ceil(3.5); echo $nr; // 4