Define Constant in Class Constructor in PHP



No, you cannot define constant in class constructor, you can use constant at the class level.

Let’s say we have the following class −

class ConstantDemo
{ }

Define a constant in the class −

class ConstantDemo {
    const LANGUAGE_NAME="PHP";
} 

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
class ConstantDemo {
   const LANGUAGE_NAME="PHP";
}
echo "The language Name is=",ConstantDemo::LANGUAGE_NAME;
?>
</body>
</html>

Output

The language Name is=PHP
Updated on: 2020-10-12T13:43:11+05:30

762 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements