Discover hidden truths with magic constants in PHP
We’ll be exploring PHP’s Magic Constants.
- __LINE__ displays the current line number of the file.
- __FILE__ displays the full path and filename of the file.
- __DIR__ displays the directory of the file.
- __FUNCTION__ displays the function name, or {closure} for anonymous functions.
- __CLASS__ displays the class name. The class name includes the namespace it was declared in.
- __TRAIT__ displays the trait name. The trait name includes the namespace it was declared in.
- __METHOD__ displays the class method name.
- __NAMESPACE__ displays the name of the current namespace.
Recap: Review where we left off last time.
https://github.com/dinocajic/php-youtube-tutorials/tree/master
https://blog.devgenius.io/php-p70-sub-namespaces-11ff9c0f0787
Magic constants aren’t complicated. The best way to illustrate them is through an example. Lets make a modification to our get_year_make_and_model() method inside of our Car class and start including some of these magic constants to see what they do.
<?php
namespace Vehicles\Cars;
//...
class Car extends \Vehicles\Vehicle
{
//...
public function get_year_make_and_model(): string
{
return $this->year . " " . $this->make . " " . $this->model;
}
}
First one we’ll look at is the __LINE__ magic constant.
<?php
namespace Vehicles\Cars;
//...
class Car extends \Vehicles\Vehicle
{
//...
public function get_year_make_and_model(): string
{
return $this->year . " " . $this->make . " " . $this->model . "<br>" .
"Line #: " . __LINE__ . "<br>";
}
}
What do you think that we get? That’s right. It’s the current line in the code. In this case, it would be “Line #: 13” since that’s the line of code that contains the __LINE__ magic constant.
<?php
namespace Vehicles\Cars;
//...
class Car extends \Vehicles\Vehicle
{
//...
public function get_year_make_and_model(): string
{
return $this->year . " " . $this->make . " " . $this->model . "<br>" .
"Line #: " . __LINE__ . "<br>" .
"File: " . __FILE__ . "<br>";
}
}
The __FILE__ magic constant returns the absolute file path where the __FILE__ magic constant was inserted. For me, it’s C:/www/youtube/php/71_Magic_Constants/Car.php.
<?php
namespace Vehicles\Cars;
//...
class Car extends \Vehicles\Vehicle
{
//...
public function get_year_make_and_model(): string
{
return $this->year . " " . $this->make . " " . $this->model . "<br>" .
"Line #: " . __LINE__ . "<br>" .
"File: " . __FILE__ . "<br>" .
"Directory: " . __DIR__ . "<br>";
}
}
The __DIR__ magic constant provides us with the directory of the file that contains the __DIR__ magic constant. It’s nearly identical to the __FILE__, but does not include the file name itself.
C:/www/youtube/php/71_Magic_Constants
<?php
namespace Vehicles\Cars;
//...
class Car extends \Vehicles\Vehicle
{
//...
public function get_year_make_and_model(): string
{
return $this->year . " " . $this->make . " " . $this->model . "<br>" .
"Line #: " . __LINE__ . "<br>" .
"File: " . __FILE__ . "<br>" .
"Directory: " . __DIR__ . "<br>" .
"Class: " . __CLASS__ . "<br>";
}
}
The __CLASS__ returns the class name that contains the __CLASS__ magic constant, which is Vehicles\Cars\Car. It gives you the full name-spaced output so that you know which class it’s actually referencing.
I think you’re starting to get the picture. If we call the __METHOD__ and the __ NAMESPACE__ magic constants, they’re going to return the method and the namespace that contain these magic constants, in this case Vehicles\Cars\Car::get_year_make_and_model and Vehicles\Cars.
<?php
namespace Vehicles\Cars;
//...
class Car extends \Vehicles\Vehicle
{
//...
public function get_year_make_and_model(): string
{
return $this->year . " " . $this->make . " " . $this->model . "<br>" .
"Line #: " . __LINE__ . "<br>" .
"File: " . __FILE__ . "<br>" .
"Directory: " . __DIR__ . "<br>" .
"Class: " . __CLASS__ . "<br>" .
"Method: " . __METHOD__ . "<br>" .
"Namespace: " . __NAMESPACE__. "<br>";
}
}
The only one that we haven’t looked at is the __TRAIT__. If we placed this in the same code, we’re not going to get anything, since this class is not a trait. However, we do have traits, such as the Engine trait where we can make a modification to its method, check_oil_level.
If we call the above code, we get Vehicles/Cars/CarParts/Engine as the output.
That’s it. I told you that it would be nice and simple. Most of the time you would use them in your log files so that you can point to errors quickly.
CULTIVATING MINIATURE WORLDS THROUGH SUB-NAMESPACE
If you can define namespaces, then it’s no different to define sub-namespaces. We’re also going to look at classes with the same name from different sub-namespaces and how we’re going to handle those naming conflicts that still might arise.
Discover hidden truths with magic constants in PHP
PHP – P71:magic constants
There are a few magic constants in PHP that we’ll go through: __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __TRAIT__, __METHOD__, and __NAMESPACE__.
PHP ERRORS SIGNAL CODE HICCUPS THAT NEED ATTENTION
Bugs are normally errors in code that the programmer coded, that produce a result, and that result is not correct.