How to define constant values in class with PHP

4 Answers

0 votes
class cl
{
    const CONSTANT = 'constant value';

    function showConstant() 
    {
        echo  self::CONSTANT . "<br />";
    }
}

echo cl::CONSTANT . "<br />";


/*
run: 

constant value

*/

 



answered Jun 4, 2016 by avibootz
0 votes
class cl
{
    const CONSTANT = 'constant value';

    function showConstant() 
    {
        echo  self::CONSTANT . "<br />";
    }
}

$classname = "cl";
echo $classname::CONSTANT . "<br />"; 


/*
run: 

constant value

*/

 



answered Jun 4, 2016 by avibootz
0 votes
class cl
{
    const CONSTANT = 'constant value';

    function showConstant() 
    {
        echo  self::CONSTANT . "<br />";
    }
}

$class = new cl();
$class->showConstant()


/*
run: 

constant value

*/

 



answered Jun 4, 2016 by avibootz
0 votes
class cl
{
    const CONSTANT = 'constant value';

    function showConstant() 
    {
        echo  self::CONSTANT . "<br />";
    }
}

$class = new cl();
echo $class::CONSTANT . "<br />";


/*
run: 

constant value

*/

 



answered Jun 4, 2016 by avibootz

Related questions

4 answers 438 views
438 views asked May 23, 2018 by avibootz
1 answer 178 views
2 answers 201 views
201 views asked Oct 13, 2016 by avibootz
4 answers 350 views
350 views asked Nov 2, 2015 by avibootz
1 answer 184 views
184 views asked May 30, 2016 by avibootz
1 answer 235 views
1 answer 214 views
...