Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Pages

Get to know 9 Magic Constants in PHP that you may often use

PHP Magic Constant
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
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

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

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
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

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

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

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

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

9. Constants ClassName::class

Almost like __TRAIT__, a constant ClassName::classwill 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

0 Komentar untuk "Get to know 9 Magic Constants in PHP that you may often use"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top