Tuesday, 26 April 2016

Namespaces in PHP

Introduction

PHP-ah hian Best Practice (Hmandan tunlai ber) tih hi sawi a awm ta fo mai a. Chutiang best practise huangah chuan NAMESPACES hi hmaih thiang lo khawp a pawimawh a ni a. Mawl te, zir tan te tana hriat fuh har ve fu si hi a ni a. Best practice han tih hian mithiam zawkte'na code dan kalphung tha bera an ngaih leh chin zui tlaka an ngaih an sawina ni berin kei chuan ka hria a. Thiam inti ve tak te chuan thenkhatin best practise an tih pawh kha best practise a ni lo ve thei tho bawk a ni. Namespaces an tih chu enge a nih a, kan hun leh tha, sum leh pai ngaihtuahin chin zui tlak a ni dawn em tih te, kan mamawh ve dawn em tih te i lo en dawn teh ang.

Definition

PHP.NET (php document inpui) ka thlir changa ka nin deuh mai chu an ziahna tawngkam vel hi a ni. Hriatthiam awl taka ziak ve mai ta lo hian technical em em hian an ziak thin a, ngeiawm ve mai mai! Mahse, PHP chungchang kan sawi dawn chuan kan rawn ber a ni lo thei si lo. Awle, PHP documentation-in Namespaces awmzia a hrilhfiah dan hi i lo en phawt teh ang.
What are namespaces? In the broadest definition namespaces are a way of encapsulating items. This can be seen as an abstract concept in many places. For example, in any operating system directories serve to group related files, and act as a namespace for the files within them. As a concrete example, the file foo.txt can exist in both directory /home/greg and in /home/other, but two copies of foo.txt cannot co-exist in the same directory. In addition, to access the foo.txt file outside of the /home/greg directory, we must prepend the directory name to the file name using the directory separator to get /home/greg/foo.txt. This same principle extends to namespaces in the programming world.

Mizo tawngin:Namespaces hi enge? Thil(code items) thliar hranna siamtu tiin huapzau awmzia kan neihtir thei a. Hei hi Programming kalkawng tinah mitthla tur a awm thei ang. Entirnan, OS hrang hrang (Windows, Macintosh, Linux, etc.) ah te hian directory(folder) hrang hrang kan nei thluah a, folder chhungah khan file kan nei leh thluah thei bawk a. Tichuan folder chhunga file te tan folder kha an namespaces a ni. A ruh langin lo sawi ta ila, file pakhat foo.txt chu /home/greg folder leh /home/other folder chhungah a awm ve ve a. /home/greg chhunga foo.txt hawng tur chuan /home/greg/foo.txt (a file hming a tawpa dahin) hmangin ka hawng thei a. Hetiang chiah hian programming khawvelah namespaces hi hman a ni ve bawk.

PHP Language-ah

PHP-ah chuan Namespaces hi Libraries leh Application siamtute'n hman nawn theih tura code elements: classes leh functions an siam laia buaina an tawh thin chinfel nana ruahman (designed) a ni.
  1. Kan code ziah-a hming in ang thliar hran nan. Third Party libraries leh function te khan kan function leh class hming ang chiahin function leh class hming an lo nei ve thei a. Midangin kan hming hman ang hmang mahse namespaces hmangin a thliar hranna awlsam tein kan siam thei tihna a ni.
  2. Class leh function hming sei pui pui tihtawina leh chhiar nuam zawka dahna a ni bawk.

Examples:

Entirna nen hian lo sawifiah ila a chiang zawk awm e:
function con( )
{ 
       echo ‘Connecting to Members’; 
}
A chunga mi ang hian function ka lo siam ta a, ka siam ang chiah hian library leh code dang ka hman te khan an lo siam ve theih avangin a pumpelh nan mahni phuahchawp liau liau namespace siamin function zz_db_connect(){ } tiin zz chu namespace leh db sub-namspace atan ka hmang ta a, hei hian hming in ang awm thei(name collisions) lakah ka code chu a humhim thei a ni. Mahse hei aia namespaces siam dan tha zawk kan nei thei a, chu chu kan thupui ber namespaces hi a ni. Code example tlem lo tarlang ve ila:
//index.php:
<?php
require_once ('func.php');
echo con();//Output: Connecting to Members
?>
A hnuaia mi ang hian func.php chu:
//func.php
<?php 
function con( )
{ 
       return 'Connetion to Members'; 
}
?>
A chunga code-ah hian index.php-ah func.php-a kan database connection chu kan lalut a. Hei hi namespaces-ah kan chantir dawn chuan a hnuaia mi ang hian tihtur a ni ang:
//func.php
<?php namespace ZZ\DB;
function con( )
{ 
       return 'Connetion to Members'; 
}
?>
tiin kan dah thei ang. Mahse index.php hi kan run chuan error a lo lang ngei ang. Chuvangin index.php-ah hian eng emaw kan thlak ve a ngai dawn a ni. Eng tak ni ang maw? Awle hetiang hian kan index.php chu kan thlak ve a ngai ang.
//index.php
<?php
require_once ('func.php');
echo ZZ\DB\con( );
?>
A chunga kan hmuh ang hian namespaces kan siam kha kan hman chhuahnaah pawh kan hman leh a ngai dawn tihna a ni. Mahse hei pawh hi function kan ngah chuan chhut ve zel a ngaih dawn avangin a lo tha tawk chuang lo. Entirnan, func.php-ah khan function query( ){ 'SELECT data'; } te hi lo neih belh leh ta ila, index.php-ah kan hman chhuah dawn chuan index.php chu a hnawk chho nghal ang:
//index.php:
<?php
require_once ('func.php');
echo ZZ\DB\con( );
echo ZZ\DB\query( );
?>
Kan hmuh ang hian ZZ\DB kan hman ve zel ngai lo hian index.php page-ah a chungber php(<?php) tag hnuaiah chiah khan namespace ZZ\DB; tiin namespace kan hawng thei a ang. Tichuan ZZ\DB hi hman nawn sek ngai lovin kan function(class pawh nise) an hming put ang ang khan kan hmang thei dawn a ni. Tichuan index.php thar chu:
//index.php
<?php
namespace ZZ\DB;
require_once ('func.php');
echo con( );
echo query( );
?>
Tih dan dang awm leh thei chu use keyword hman hi a ni. use keyword kan hman chuan a sub-namespace kha function-ah kan beitir zel a ngai thung ang. Entirnan:
//index.php:
<?php
require_once ('func.php');
use ZZ\DB;
echo DB\con( );
echo DB\query( );
?>
References:
  1. Namespaces in php
  2. Namespaces Overview
  3. Defining Namespaces

No comments :

Post a Comment