(PHP 5 >= 5.3.0, PHP 7, PHP 8) Before discussing the use of namespaces, it is important to understand how PHP knows which namespaced element your code is requesting. A simple analogy can be made between PHP namespaces and a filesystem. There are three ways to access a file in a file system:
The same principle can be applied to namespaced elements in PHP. For example, a class name can be referred to in three ways:
Here is an example of the three kinds of syntax in actual code: file1.php
FOO = 1; file2.php
FOO = 2; Note that to access any global class, function or constant, a fully qualified name can be used, such as \strlen() or \Exception or Example #1 Accessing global classes, functions and constants from within a namespace
strlen() {} richard at richard-sumilang dot com ¶ 14 years ago
Object{
Anonymous ¶ 7 years ago
// Something awful here Lukas Z ¶ 10 years ago
tom at tomwardrop dot com ¶ 10 years ago
MyProject\People { philip dot preisser at arcor dot de ¶ 11 years ago
main\sub1 Apa itu namespace di PHP?Namespaces PHP
Namespaces adalah kualifikasi yang menyelesaikan dua masalah berbeda, yaitu: Mereka memungkinkan pengorganisasian yang lebih baik dengan mengelompokkan kelas-kelas yang bekerja sama untuk melakukan tugas. Mereka memungkinkan nama yang sama digunakan untuk lebih dari satu kelas.
Apa fungsi use pada PHP?Kata kunci use memiliki dua tujuan: memberi tahu kelas untuk mewarisi sifat dan memberikan alias ke namespace.
|