Friday 20 October 2017

Advanced Namespace in PHP

Namespace chungchang hi post hmasa lamah khan kan cover nual tawh a. Tun tumah hian PHP7-a a lo danglam tak dan chungchang tlem zawng han thur chhuah leh zeuh ka tum dawn a ni. Post hmasa lama mi kha chu a basic leh sawifiahna angah ngai ila, tun tuma mi hi chu PHP 7 hnuai lama hman theih loh mahse PHP7 hmang pha chin tan chuan thiltih ti awlsam em em tu a ni ang. Kan hriat angin __autoload(), class lakluh nana kan hman thin chu PHP 7.2 atang chuan remove a ni ta, spl_autoload_register() erawh a la tha viau a ni ang, an la paih em lo. Tun tuma tan chuan require_once leh phawt ang. 
PHP5-ah chuan namespace-a class kan lakluh dawn hian post hmasaa kan sawi tawh ang khan use declaration kan hman ngei ngei a ngai a. Class panga vel pawh lo lalut (import) dawn i la, use declaration hi voi nga kan hman a ngai tihna a ni. Mahse vanneihthlak takin PHP7 ah chuan use declaration hi group theih a lo ni ta. PHP5 lama kan hman dan lo en hmasa ta ila. Example atan hian file pathum ka hmang a, chung te chu: index.php, Php.php leh Javascript.php te an ni a, folder thuhmunah ka dah vek a ni.
index.php
<?php 
require_once 'Php.php';
require_once 'Javascript.php';
use Mizo\Web\Php\Website;
use Mizo\Web\Php\PHP;
use Mizo\Web\Php\Uprint;
use Mizo\Web\JS\Javascript;
new Javascript();
echo Website::$site;
echo PHP::$php;
echo Uprint::printUnicode("U+20B9");
?>
Php.php

<?php 
namespace Mizo\Web\Php;
class PHP{
    public static $php = "Hypertext Preprocessor\n";
}
class Website {
   public static $site = "Joomla, Wordpress and Drupal are using PHP\n";
}
interface UnicodePrinter{
   public static function printUnicode($code);
}
class Uprint implements UnicodePrinter{
     public static $char = null;
     public static function printUnicode($code){ 
           self::$char = preg_replace('/U\+([0-9A-F]{4,5})/', '&#x\\1;', $code);
     return self::$char? "$code => ".html_entity_decode(self::$char, ENT_NOQUOTES,'UTF-8')."\n" : "No Input";
     }
}
?>
Javascript.php

<?php
namespace Mizo\Web\JS;
class Javascript{
 public function __construct()
 {
   echo "Javascript is the language of modern browsers\n";
 }
}
const BUAIKUM = "Buai Kum: 1966\n";
const MAUTAM = "Mautam Kum: 1950\n";
function printTotal(...$num){
 return implode('+',$num)." => ".array_sum($num)."\n";
}

function printList($items){
 return "<ol>\n<li>".implode("</li>\n<li>", $items)."</li>\n</ol>\n";
}
?>
A chunga code kan hman te hi PHP5 leh PHP7-ah hman theih ve ve a ni a, buaina em em a awm hran lo. index.php-a kan hmuh ang hian Class tin hman chhuah nan use keyword kan hman zel a ngai a. Hei hi PHP7-ah chuan group dan thar a awm a ni. use keyword lai hi lo tarlang leh ila:
use Mizo\Web\Php\Website;
use Mizo\Web\Php\PHP;
use Mizo\Web\Php\Uprint;
use Mizo\Web\JS\Javascript;
Mizo\Web\PHP hi pathum lai kan nei a, hei hi voi thum chhu nawn kher lovin a hnuaia mi ang hian PHP7-ah chuan tih theih a ni.
use Mizo\Web\Php\ {Website, PHP, Uprint};
use Mizo\Web\JS\Javascript;
Kan hmuh ang hian Class hming te theuh hi { } chhungah dahin coma(,)-in kan daidang a. Hetiang hian namespace thuhmuna class awm te chu kan dah thei a ni. A tawi thianghlim fel ta at mai. Tin use keyword hi voi khat chiah kan hmang duh a nih chuan a hnuaia mi ang hian kan group leh thei bawk a ni.
use Mizo\Web\ {
   Php\Website, //Alias Php\Website AS Site tiin dah theih a ni ve bawk
   Php\PHP,
   Php\Uprint,
   JS\Javascript
}
Hetiang deuh chiah hian const (constant) leh function te pawh lakluh ve theih a ni bawk. Javascript.php ah hian const leh function kan nei a. A hnuai mi ang hian kan lalut thei ang.
use function Mizo\Web\JS\ {printTotal,printList};
use const Mizo\Web\JS\ {BUAIKUM, MAUTAM};
Kan duh phawt chuan use Declaration pakhat chauh hmangin a vaiin kan la lut thei vek bawk a:
use Mizo\Web\ {
   Php\Website,
   Php\PHP,
   Php\Uprint,
   JS\Javascript, 
   function JS\printTotal, 
   function JS\printList, 
   const JS\BUAIKUM, 
   const JS\MAUTAM
};
//a tawpa semi-colon te hi theihnghilh a awl reuh hle.
use keywork hman tluk tluk ngai lovin ti khan kan duh ang namespace chu kan la lut vek thei ta. A tawp nan kan unicode character print-na hi PHP7 feature null coalescing milin in dahrem leh zuai teh ang:
return "$code => ".html_entity_decode(self::$char, ENT_NOQUOTES,'UTF-8')."\n" ?? "No Input"; 
#Index.php
<?php 
require_once 'Php.php';
require_once 'Javascript.php';
use Mizo\Web\ {
   Php\Website,
   Php\PHP,
   Php\Uprint,
   JS\Javascript, 
   function JS\printTotal, 
   function JS\printList, 
   const JS\BUAIKUM, 
   const JS\MAUTAM
};
new Javascript();
echo Website::$site;
echo PHP::$php;
echo Uprint::printUnicode("U+20B9");
echo printTotal(2,3,4);
echo BUAIKUM;
echo MAUTAM;
$project = ['Presentation Sekibuhchhuak','Pocket Store','Homework Solver'];
echo printList($project);
?>
Output:
Javascript is the language of modern browsers
Joomla, Wordpress and Drupal are using PHP
Hypertext Preprocessor
U+20B9 =&gt; ₹
2+3+4 =&gt; 9
Buai Kum: 1966
Mautam Kum: 1950
<ol>
    <li>Presentation Sekibuhchhuak</li>
    <li>Pocket Store</li>
    <li>Homework Solver</li>
</ol>
Source: PHP.NET Note: Test vel nan SublimeText Build Console ka hman avangin \n hi ka dah deuh vek a ni. Image: bccfalna.com

No comments :

Post a Comment