I can declare constants that are used in that class.
To declare a constant, use the const keyword. Constants are declared in uppercase letters. Of course it's a promise.
const constant name = value;
self::constant name;
So let's declare and use a constant
<?php class Car { const TRUNK = true; public function checkTrunk() { return self::TRUNK; } } $car = new Car; if($car->checkTrunk() == true){ echo "This car has a trunk." ; } else { echo "This car has no trunk." ; } ?>
Result
To use a constant from a class outside the class, use instance ::.
Instance :: constant name;
<?php class Car { const TRUNK = true; public function checkTrunk() { return self::TRUNK; } } $car = new Car; if($car::TRUNK == true){ echo "This car has a trunk."; } else { echo "This car has no trunk."; } ?>
Of course, you must declare an instance so that you can use class constants outside the class.
Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.
//=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']?>ALL COMMENTS 0
Sort by