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:
Why are the results
3
?
Because the location of constants
__LINE__
is in the 3rd row. We can see this with the text editor.
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:
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:
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:
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:
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:
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:
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:
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:
0 Komentar untuk "Get to know 9 Magic Constants in PHP that you may often use"
Silahkan berkomentar sesuai artikel