is the first time that uses Namespaces in a project. I have 3 classes:
Contacto.class.php
/**
* Gestiona las operaciones ABM de la informacion de contacto de un usuario
* cualquiera.
* La clase no realiza validaciones, eso será labor del controlador.
* @package AdminManantiales
* @subpackage Abm
* @author Ramiro Martínez D'Elía
*/
namespace AdminManantiales\Classes\Abm\Contacto;
class Contacto { // Implement ... }
Usuario.class.php
/**
* Gestiona las operaciones ABM del tipo de usuario más abstracto.
* La clase no realiza validaciones, eso será labor del controlador.
* @package AdminManantiales
* @subpackage Abm
* @author Ramiro Martínez D'Elía
*/
namespace AdminManantiales\Classes\Abm\Usuario;
abstract class Usuario { // Implement ... }
Alumno.class.php
/**
* Gestiona las operaciones ABM del tipo de usuario alumno.
* La clase no realiza validaciones, eso será labor del controlador.
* @package AdminManantiales
* @subpackage Abm
* @author Ramiro Martínez D'Elía
*/
namespace AdminManantiales\Classes\Abm\Alumno;
class Alumno extends \AdminManantiales\Classes\Abm\Usuario{ // Implement ... }
The path of the classes is ROOT/classes/Abm
. So, when include the classes to work:
include($_SERVER['DOCUMENT_ROOT']."/classes/Abm/Contacto.class.php");
include($_SERVER['DOCUMENT_ROOT']."/classes/Abm/Usuario.class.php");
include($_SERVER['DOCUMENT_ROOT']."/classes/Abm/Alumno.class.php");
Fails with the next message: Class 'AdminManantiales\\Classes\\Abm\\Usuario' not found in <b>/media/Datos/www/AdminManantiales/classes/Abm/Alumno.class.php</b> on line <b>12</b>
Any ideas ?.
When defining namespace you shouldn't use class name in it.
Class with definition:
namespace AdminManantiales\Classes\Abm;
abstract class Usuario { // Implement ... }
will be accessible after using
use AdminManantiales\Classes\Abm\Usuario;
or
class Alumno extends \AdminManantiales\Classes\Abm\Usuario { // Implement ... }
To explain it better: namespace can be considered as bag and class will be item in that bag. To access selected item you have to provide name of the bag ( AdminManantiales\\Classes\\Abm
) and this item ( Usuario
) :)
You are putting each class in its own namespace because you are including the class name in the namespace name.
Instead of doing this, do this for all your classes:
namespace AdminManantiales\Classes\Abm;
This will also let you simplify the syntax. Now that the classes are in the same namespace you don't need to refer to them with fully qualified names, so this will work:
class Alumno extends Usuario { ... }
Never use slashes and dot in namespace declaration.
wrong formats:
<?php
namespace first\second.w;
?>
<?php
namespace first/second;
?>
check here php.net
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.