![PHP Magic Constant](https://d33wubrfki0l68.cloudfront.net/e5585e1ea24006cc8379f9da80e796bdb792d1c4/df530/img/php/php.png)
PHP has many constants that are ready to use (predefined) , without having to be created.
A constant is a value that will never change and cannot be changed.
However, there are 9 magic constants in PHP that will always change in value depending on their use.
What are they?
Let's discuss ...
1. Constants __LINE__
This constant will contain a value that states the current line number.
Example:
<?php
echo "Ini beris pertama<br>";
echo "Ini baris ke: " . __LINE__."<br>";
echo "ini baruus ketiga<br>";
?>
The result:
![Results of Magic Constants LINE](https://d33wubrfki0l68.cloudfront.net/70d367f379879c62926f04429fee4c2faeef6f43/a31d6/img/php/magic-constants/hasil-magic-line.png)
Why are the results
3
?
Because the location of constants
__LINE__
is in the 3rd row. We can see this with the text editor.![Results of Magic Constants LINE](https://d33wubrfki0l68.cloudfront.net/82e065d0222c99cb678670e874cfda3bb196bcbf/3c884/img/php/magic-constants/vim-editor.png)
2. Constants __FILE__
This constant will contain the full address (path) of the PHP file.
Example:
<?php
echo "File ini berada di: " . __FILE__;
?>
The result:
![Results of Magic Constants FILE](https://d33wubrfki0l68.cloudfront.net/10867bc760fa45c35d0bcbc567a6c0b5dd16d1b6/2a259/img/php/magic-constants/hasil-magic-file.png)
3. Constants __DIR__
Similar to constants
__FILE__
, constants __DIR__
will contain directory addresses from PHP files.
Example:
<?php
echo "File ini berada di direktori: " . __DIR__;
?>
The result:
![Results of Magic Constants DIR](https://d33wubrfki0l68.cloudfront.net/9935a049c14b66011460546c0c2f22a6da25c30f/29284/img/php/magic-constants/hasil-magic-dir.png)
The constellation is
__DIR__
usually used to import other files like this:<?php include __DIR__."/config.php"; ?>
4. Constants __FUNCTION__
This constant will contain the name of the function.
Example:
<?php
function aku_lapar() {
echo "Nama fungsi ini adalah: " . __FUNCTION__;
}
// panggil fungsi
aku_lapar();
?>
The result:
![Results of Magic Constants FUNCTION](https://d33wubrfki0l68.cloudfront.net/5ea4804243fe8a05f1a4312f9c67020daedb9032/127fa/img/php/magic-constants/hasil-magic-function.png)
5. Constants __CLASS__
The constant
__CLASS__
will contain the name of the class .
Example:
<?php
class ManusiaSerigala {
function printClassName(){
echo "Nama class adalah: ".__CLASS__;
}
}
// membuat objek
$aku = new ManusiaSerigala();
$aku->printClassName();
?>
The result:
![Results of Magic Constants CLASS](https://d33wubrfki0l68.cloudfront.net/7d3e723fe4803d52049470a99d31b873c346c417/bad39/img/php/magic-constants/hasil-magic-class.png)
6. Constants __TRAIT__
The constant
__TRAIT__
will contain the name of the trait and namespace .
Example:
<?php
// membuat namespace
namespace Enemy;
// membuat trait
trait Makhluk {
function printTrait() {
echo "Nama Trait adalah: ".__TRAIT__;
}
}
class ManusiaSerigala {
use Makhluk;
}
// membuat objek
$aku = new ManusiaSerigala();
$aku->printTrait();
?>
The result:
![TRAIT Magic Constants Results](https://d33wubrfki0l68.cloudfront.net/1fda86fb0fe4d2fa46161b94d7c1ead943ddaeef/2e9aa/img/php/magic-constants/hasil-magic-trait.png)
7. Constants __METHOD__
Constants are the
__METHOD__
same as constants __FUNCTION__
. It will contain the name of the method and class where it is used.
Example:
<?php
class ManusiaSerigala {
function lari(){
echo "ini adalah method: ".__METHOD__."<br>";
}
function lompat(){
echo "ini adalah method: ".__METHOD__."<br>";
}
}
// membuat objek
$aku = new ManusiaSerigala();
$aku->lari();
$aku->lompat();
?>
The result:
![Results of Magic Constants METHOD](https://d33wubrfki0l68.cloudfront.net/d565e8f70fbd84b7116925bd951bd60819a54702/188a5/img/php/magic-constants/hasil-magic-method.png)
8. Constants __NAMESPACE__
The constant
__NAMESPACE__
will contain the namespace name .
Example:
<?php
// membuat namespace
namespace Enemy;
class ManusiaSerigala {
function printNamaNamespace(){
echo "Nama namespace adalah: ".__NAMESPACE__;
}
}
// membuat objek
$aku = new ManusiaSerigala();
$aku->printNamaNamespace();
?>
The result:
![Results of Magic Constants NAMESPACE](https://d33wubrfki0l68.cloudfront.net/50118e2f07b1376ff27706f0bc07ad835cb7dc84/34b35/img/php/magic-constants/hasil-magic-namespace.png)
9. Constants ClassName::class
Almost like
__TRAIT__
, a constant ClassName::class
will contain the name of the class and namespace .
Example:
<?php
namespace Makhluk {
class ManusiaSerigala {
}
echo "Nama class adalah: ".ManusiaSerigala::class;
}
?>
The result:
![Magic Constants Results ClassName :: class](https://d33wubrfki0l68.cloudfront.net/a9302f50c00583e55327204ae38e77c075b3380a/6ca06/img/php/magic-constants/hasil-magic-class-2.png)
0 Komentar untuk "Get to know 9 Magic Constants in PHP that you may often use"
Silahkan berkomentar sesuai artikel