#10 Theme Development | namespace in php
namespaces
- A way of encapsulating item
アイテムをカプセル化する方法 - like a virtual folder or directory defined with namespace keyword at the top of the class file followed by the name you like
好きな名前で定義された仮想フォルダーor ディレクトリ - Allow you to have two or more classes with the same name in different namespaced directories
同じクラス名で、違うnamespaceのディレクトリを複数持つことができる
standard way
class Product{} // define
$product = new Product(); // use
一般的な方法は、製品のクラスを定義し、new keywordを使用しインスタンス化します
namespaces
namespace App;
class Product{}
$product = new App\Product();
or
use App,
$product = new Product();
namespaceの場合、最初にnamespace と、与えたいキーワードを書きます。
これは実際にあるディレクトリではなく、仮想のディレクトリです。
その後、クラスを定義し、インスタンス化します。
そのキーワードを前に付けたい場合、
$product = new App\Product();
のように、namespace + バックスラッシュを書きます。
長い名前を付与したい場合は
use App,
$product = new Product();
use と、namespaceを書き、インスタンス化します。
こうすると実際に頭にnamespaceを書く必要はないです。