namespace -k klasiek kapsuleteko erabiltzen da, adibidez izen bardiñeko klasiek erabiltzeko baie erabilera ezberdiñetarako, php-n 5.3-tik aurrera erabili lei:
Ikusten dun moduen izen bardiñeko klasiek dakuz, zelan erabili?
lib/login.class.php
<?php namespace lib; class login { public function __construct() { echo "lib: login\n"; } } ?>
mvc/model/login.class.php
<?php namespace model; require_once('lib/login.class.php'); class login { public function __construct() { $login = new \lib\login(); echo "model: login\n"; } } ?>
mvc/controller/login.class.php
<?php namespace controller; require_once('mvc/model/login.class.php'); class login { public function __construct() { $login = new \model\login(); echo "controller: login\n"; } } ?>
index.php
<?php require_once('mvc/controller/login.class.php'); header("Content-Type:text/plain"); $login = new \controller\login(); ?>
Zelan deitsu core-ko klasieri?
lib/login.class.php
<?php namespace lib; class login { public function __construct() { echo "lib: login\n"; new DOMDocument(); } } ?>
ERRORIE:
PHP Fatal error: Class 'lib\\DOMDocument' not found in lib/login.class.php on line 8
Hau konponduteko \ bat sartun hasikeratan:
lib/login.class.php
<?php namespace lib; class login { public function __construct() { echo "lib: login\n"; new \DOMDocument(); } } ?>
index.php nabegatzailien:
lib: login model: login controller: login