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 637 views
637 views asked May 23, 2018 by avibootz
1 answer 275 views
2 answers 310 views
310 views asked Oct 13, 2016 by avibootz
4 answers 534 views
534 views asked Nov 2, 2015 by avibootz
...