CKTIS / PHP
Sleutelwoord const
Declareert een benoemde constante zonder dollarteken met een constante expressie.
Versie: PHP 8.4; repository runtime, production runtime not attested
Syntax
const TAX = 2; class Invoice { public const int SCALE = 100; }Declareert een benoemde constante zonder dollarteken met een constante expressie. Een namespaceconstante lees je op naam, een klasseconstante met :: volgens de zichtbaarheid. Sinds PHP 8.3 kunnen klasseconstanten een type hebben. Hertoewijzing is uitgesloten. Een subklasse kan een niet-finale constante herdeclareren. Het type mag covariant smaller worden. final verhindert overschrijven.
namespace Billing;
const TAX=2; class Invoice { public const int SCALE=100; }
echo TAX*Invoice::SCALE;
class BaseCode {public const int|string CODE="v";final public const int REV=2;} class NumericCode extends BaseCode {public const int CODE=3;} echo "\n",json_encode([BaseCode::CODE,NumericCode::CODE,NumericCode::REV]);
200
["v",3,2]
Voorbeelden
<?php
namespace Billing;
const TAX=2; class Invoice { public const int SCALE=100; }
echo TAX*Invoice::SCALE;
class BaseCode {public const int|string CODE="v";final public const int REV=2;} class NumericCode extends BaseCode {public const int CODE=3;} echo "\n",json_encode([BaseCode::CODE,NumericCode::CODE,NumericCode::REV]);
Resultaat
200 ["v",3,2]