DEV Community

Cover image for Our way: Constants
Robson Tenório
Robson Tenório

Posted on

Our way: Constants

👉 Go to Summary

There is no wrong or right way, there is our way.

-- Someone

.
.
.

Magic number

WTF!?

class Costumer 
{
   public float $amount;

   public function something() 
   {
       // Ok, and WTF means 98.45 ?
       if ($this->amount > 98.45) {
          ...
       }

   }

}

Enter fullscreen mode Exit fullscreen mode

Nice!

class Costumer 
{
   public float $amount;

   public const LIMIT = 98.45

   public function something() 
   {
       if ($this->amount > self::LIMIT) {
          ...
       }

   }

}
Enter fullscreen mode Exit fullscreen mode

Nice!

// If LIMIT is used across codebase put it on `.env`
LIMIT = 98.45


public function something() 
{
    if ($this->amount > config("app.limit")) {
          ...
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)