Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to declare and use constants in class with PHP

2 Answers

0 votes
class ConstClass
{
    const PI = M_PI;
    const PI_2 = M_PI_2;
    const COST = 9.95;
}

$val = ConstClass::PI * ConstClass::PI_2 * ConstClass::COST;

echo $val;

 
/*
run: 
  
49.10128189542
  
*/    

 



answered Sep 6, 2017 by avibootz
edited Sep 7, 2017 by avibootz
0 votes
class ConstClass 
{
    const COST = 9.95;

    public function getCost($units) 
    {
         return self::COST * $units;
    }
}

$object = new ConstClass;

echo $object->getCost(10);

 
/*
run: 
  
99.5
  
*/                    

 



answered Sep 7, 2017 by avibootz

Related questions

1 answer 113 views
113 views asked Nov 29, 2023 by avibootz
1 answer 163 views
1 answer 207 views
1 answer 177 views
1 answer 213 views
213 views asked Jun 5, 2016 by avibootz
...