PHP BOOTCAMP: Magic Constants
There are a lot of methods, classes and constants that PHP offers to developers for use.
We’ll talk about the MAGIC CONSTANTS today.
Note: These constants didn’t just appear in the standard PHP library, they were not dropped from heaven and a PHP developer doubling as a magician also didn’t write them. This was actually intended to be funny, so if you didn’t laugh, I might have to do a tutorial on recognizing humorous materials
There are about 8 useful and important MAGIC CONSTANTS you can use in PHP
1. __FILE__ – This returns the name and full path of the current file
2. __DIR__ – This returns the current directory
3. __TRAIT__ – This returns the trait name including the namespace it was defined in.
4. __NAMESPACE__ – This returns the namespace of the current file/script
5. __LINE__ – This returns the line number where this constant was used.
6. __METHOD__ – This returns the class method name
7. __CLASS__ – This returns the current class name
8. __FUNCTION__ – This returns the function name as it was declared where this constant was used.
CODE TIME
Create a file named magicConstant.php
magicConstant.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php namespace Busayo; class MagicConstants { public function getFile() { return __FILE__; } public function getDirectory() { return __DIR__; } public function getTrait() { return __TRAIT__; } public function getNamespace() { return __NAMESPACE__; } public function getLine() { return __LINE__; } public function getMethod() { return __METHOD__; } public function getClass() { return __CLASS__; } public function getFunction() { return __FUNCTION__; } } $app = new MagicConstants; echo $app->getFile(). PHP_EOL; echo $app->getDirectory(). PHP_EOL; echo $app->getTrait(). PHP_EOL; echo $app->getNamespace(). PHP_EOL; echo $app->getLine(). PHP_EOL; echo $app->getMethod(). PHP_EOL; echo $app->getClass(). PHP_EOL; echo $app->getFunction(). PHP_EOL; |
Run the code and check the output
This is mine
Look at the results carefully. One output is missing. Can you guess?
Yes!!!, the output of the getTrait()
is empty. We have no trait in this class hence no result for that.
Write a trait and write a function to use the __TRAIT__ magic constant.
Check out the source code here
Please if you have any questions or observations, feel free to drop it in the comments section below.

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016