PHP Namespace

 Namespaces are programming language mechanism for organizing function, variable and classes. PHP 5.3 add support for namespace. Namespaces solve two different problems.

1-  They allow for better organization by grouping classes that work together to perform a task

2- They allow the same name to be used more than one class

Example - you may have a set of classes which describe an HTML table, such as Table, Row and Cell while also having another set of classes to describe furniture, such as Table, Chair and Bed. Namespaces can be used to organize the classes into two different groups while also preventing the two classes Table and Table from being mixed up.

Declaring a namespace - 

namespace are declared at the beginning of a file sing the namespace keyword.

syntax - 

namespace HTML;

Note - 

 A namespace declaration must be the first thing in the PHP file. The following code would be invalid:

<?php

echo "Hello PHP";

namespace HTML;

.........

?>

Constants, classes and functions declared in this file will belong to the Html namespace:

Example - 

Create a Table class in the Html namespace:

<?php
namespace Html;
class Table {
  public $title = "";
  public $numRows = 0;
  public function message() {
    echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";
  }
}
$table = new Table();
$table->title = "My table";
$table->numRows = 5;
?>

<!DOCTYPE html>
<html>
<body>

<?php
$table->message();
?>

</body>
</html>

For further organization, it is possible to have nested namespaces:

syntax - 

Declare a namespace called Html inside a namespace called Code:

namespace Code\Html;

Using Namespaces - 

Any code that follows a namespace declaration is operating inside the namespace, so classes that belong to the namespace can be instantiated without any qualifiers. To access classes from outside a namespace, the class needs to have the namespace attached to it.

Example - 

Use classes from the Html namespace:

$table = new Html\Table()
$row = new Html\Row();

When many classes from the same namespace are being used at the same time, it is easier to use the namespace keyword:

Example - 

Use classes from the Html namespace without the need for the Html\qualifier:

namespace Html;
$table = new Table();
$row = new Row();

Namespace Alias - 

It can be useful to give a namespace or class an alias to make it easier to write. This is done with the use keyword:

Example -

given a namespace alias 

use Html as H;
$table = new H\Table();


---------------------------

use Html\Table as T;
$table = new T();


1 comment:

Devstree IT Services said...

Wowww... What a blog! The best thing about your sharing and posting is that you always provide content that is helpful. Anybody want to build your website. We are one of the Best Web Development Company In India - Devstree IT Services Pvt. Ltd. which provides Web Development Services for building with perfection including high-end and complex functionalities. We have one of best blog which contains fruitful information of app & web development services. Follow us at: https://devstreeitservices.blogspot.com/

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...