Namespaces are one of the most significant changes in PHP 5.3.Namespaces are a way of encapsulating items.
As the size of your PHP code library increases, the more likely you will accidentally reuse a function or class name that has been declared before. The problem is exacerbated if you attempt to add third-party components or plugins.
Name collision problems can be solved with namespaces. PHP constants, classes, and functions can be grouped into namespaced libraries.
Let’s check some example.
Below example clarify that how to use namespace and how to define class in namespace how to call it.
file1:
<?php namespace foo; class Cat { static function says() {echo 'meoow'; } }
file2:
<?php namespace bar; class Dog { static function says() {echo 'ruff'; } }
file3:
<?php namespace other\animate; class Animal { static function breathes() {echo 'air'; } }
file4:
<?php namespace fub; include 'file1.php'; include 'file2.php'; include 'file3.php'; use foo as feline; use bar as canine; use other\animate; //use other\animate as animate; echo feline\Cat::says(), "<br ?-->\n"; echo canine\Dog::says(), "\n"; echo \animate\Animal::breathes(), "\n";
Please follow and like us: