PHP Classes and Objects Defining Class Constants - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Classes and Objects Defining Class Constants - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, May 26, 2019

PHP Classes and Objects Defining Class Constants



PHP Classes and Objects




Defining Class Constants

Problem

You want to define constants on a per-class basis, not on a global basis.

Solution

Define them like class properties, but use the const label instead:

         class Math {
               const pi = 3.14159; // universal
               const e = 2.71828;  // constants
         }

         $area = math::pi * $radius * $radius;

Discussion

PHP reuses its concept of global constants and applies them to classes. Essentially, these are final properties.

Declare them using the const label:

         class Math {
               const pi = 3.14159; // universal
               const e = 2.71828;  // constants
         }

         $area = math::pi * $radius * $radius;

Like static properties, you can access constants without first instantiating a new instance of your class, and they’re accessed using the double colon (::) notation. Prefix the word self:: to the constant name to use it inside of a class.

Unlike properties, constants do not have a dollar sign ($) before them:

         class Circle {
               const pi = 3.14159;
               protected $radius;

               public function __construct($radius) {
                      $this->radius = $radius;
               }

               public function circumference() {
                      return 2 * self::pi * $this->radius;
               }
         }

         $c = new circle(1);
         print $c->circumference();

This example creates a circle with a radius of 1 and then calls the circumference method to calculate its circumference:

         define('pi', 10); // global pi constant

         class Circle {
               const pi = 3.14159; // class pi constant
               protected $radius;

               public function __construct($radius) {
                      $this->radius = $radius;
               }

               public function circumference() {
                      return 2 * pi * $this->radius;
               }
         }

         $c = new circle(1);
         print $c->circumference();

Oops! PHP has used the value of 10 instead of 3.14159, so the new answer is 20 instead of 6.28318. Although it’s unlikely that you will accidentally redefine Ï€ (you’ll probably use the built-in M_PI constant anyway), this can still slip you up.

You cannot assign the value of an expression to a constant, nor can they use information passed into your script:

         // invalid
         class permissions {
               const      read = 1 << 2;
               const     write = 1 << 1;
               const execute = 1 << 0;
         }

         // invalid and insecure
         class database {
               const debug = $_REQUEST['debug'];
         }

Neither the constants in permissions nor the debug constant in database are acceptable because they are not fixed. Even the first example, 1 << 2, where PHP does not need to read in external data, is not allowed.

Because you need to access constants using an explicit name, either self:: or the name of the class, you cannot dynamically calculate the class name during runtime. It must be declared beforehand. For example:

         class Constants
               const pi = 3.14159;

               // rest of class here
         }

         $class = 'Constants';

         print $class::pi;

This produces a parse error, even though this type of construct is legal for nonconstant expressions, such as $class->pi.


No comments:

Post a Comment

Post Top Ad