Secret Ways to Know Namespaces in PHP

Assalamualaikum,

Hi All friends, thanks for coming back again at Sispendik.net

Do you want to Know the Secrets of the Namespace on PHP? Nah, Frand8K will give his knowledge to friends, keep watching..

Namespace is a group or group of entities (class, object, function) grouped under one name. The namespace in PHP also describes the abstract directory of a file. The PHP namespace does not describe the directory where the file is stored. The use of namespaces has many benefits. We can create a class, interface, function uniquely by using namespace.

If you make a small project and for personal, maka kesalahan nama atau nama yang duplikate tidak akan menjadi permasalahan yang serius, tetapi jika anda ingin membuat project yang besar maka kesalahan ini akan menjadi sesuatu yang sangat serius. Anda tidak akan bisa mengontrol Class, Interface, Functions and more, if you use Global Namespace.

  1. Namespace declaration

Untuk membuat namespace sama halnya dengan membuat code PHP biasa yang diawali dengan tag <?php and starts with the key word(the word key) namespace followed by its NamePace name and ends with a semicolon ( ; ). see code below:

1.<?php
2.        namespace Namanamespace
3         //tulis kode disini

  1. Sub Namespace

Untuk membuat membuat subnamespace kita bisa membuat dengan method menambahkan karakter \ after the first namespace, example :

1.<?php
2.        namespace Namanamespace\Namakedua;
3          //tulis kode disini

Another way to use namespace is by using Kurung Kurawal ( { } )

1.<?php
2.        namespace CaraLain{
3.        //tulis kode disini
4.      }

  1. Global namespace

Global namespace created without using a name, just use namespace keywords, as shown below:

1.<?php
2.         namespace{
3.                         //tulis kode disini
4.         }
5.    ?>

Create more than one namespace, the way as below:

1.<?php
2.         namespace Blog\Frand8K{
3.                         //tulis kode disini
4.         }
5.          namespace You\Blog{
6.         }
7 ?>

  1. Using namespace

The first file with the name Modern1.php, namespace Frand8KKisaran, Class Frand8K Munawir

1.<?php
2.        include “Modern1.php;
3.        use ModernFrand8K\kisaran\frand8kMunawir as Frand8K;
4.        class Testing{
5.        private $test;
6.        public function __construct(){
7.                $this ->test=new Frand8K;
8.                echo “Nama : “.$this ->test ->test(“Frand8K Munawir”);
9.                echo “<br/>”;
10.              echo “Alamat : “.$this ->test ->test2(“Banda Aceh”);
11.               }
12.       }
14.?>

File kedua dengan nama Testing.php , Nama class Testing, and storage in the same folder:

1.<?php
2.        include “Modern1.php;
3.        use ModernFrand8K\kisaran\frand8kMunawir as Frand8K;
4.        class Testing{
5.        private $test;
6.        public function __construct(){
7.                $this ->test=new Frand8K;
8.                echo “Nama : “.$this ->test ->test(“Frand8K Munawir”);
9.                echo “<br/>”;
10.              echo “Alamat : “.$this ->test ->test2(“Banda Aceh”);
11.               }
12.       }
13.       $jalan=new Testing();
14.?>

Output Program :

Coding Frand8K 1 300x155 - Cara Rahasia Mengenal Namespace di PHP

  • Aliasing or Importing

Aliasing or importing in the PHP programming language can also be done using the use keyword. Use this will enter the namespace needed into the program being created. example :

1.<?php
2.        include “Modern1.php;
3.        use ModernFrand8K\kisaran\frand8kMunawir as Frand8K;
4.        class Testing{
5.        private $test;
6.        public function __construct(){
7.                $this ->test=new Frand8K;
8.                echo “Nama : “.$this ->test ->test(“Frand8K Munawir”);
9.                echo “<br/>”;
10.              echo “Alamat : “.$this ->test ->test2(“Banda Aceh”);
11.               }
12.       }
13.       $jalan=new Testing();
14.?>

Conclusion

Namespaces are a very useful feature in organizing inner components application to avoid errors due to class or function duplication. You will rely heavily on this feature while the applications you create are increasingly complex and involve many third party libraries, so immediately get used to using the namespace from now on.

Hopefully this is useful and if there are deficiencies, please add them to improve

Hope this can help you to become a real programmer. So that's the discussion of namespace secrets in PHP. Quite easy is not it? And hopefully this article can be useful for all of you. Warm greetings from Frand8K, which you are still Learning to understand in Technology Now.

Similar Posts:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *