2

I have an class called Alumno.class.php, locate in Root\Classes\Abm\Alumno.class.php. So this class header is:

/** * 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; class Alumno extends Usuario{ // Implement } 

Now, I need to use the class in a php script, and try with this:

use \AdminManantiales\Classes\Abm\Alumno as AbmAlumno; [...] // Proceso el alta. $alumno = new AbmAlumno(); $alumno->alta($_POST); $nombreCompleto = $alumno->toStr(); 

But it fails in the $alumno = new AbmAlumno(); line. With the next message:

Class 'AdminManantiales\Classes\Abm\Alumno' not found

How do I include correctly the class using the "use" keyword ?.

4
  • are you using composer ? Commented Sep 18, 2013 at 14:16
  • Do you have an autoloader set up or are including the file containing the class when you are instantiating it? Commented Sep 18, 2013 at 14:16
  • 1
    just a note: We have the same last name! :P Commented Sep 18, 2013 at 14:17
  • 4
    use does not import / include anything. You still have to require the file if you aren't using an autoloader. Commented Sep 18, 2013 at 14:17

2 Answers 2

5

The use keyword doesn't actually do anything. You'll have to either include the PHP script manually, using include \AdminManantiales\Classes\Abm\Alumno.php (or whatever the filepath is) or use autoloading like this,

function autoload($classId) { $classIdParts = explode("\\", $classId); $classIdLength = count($classIdParts); $className = strtolower($classIdParts[$classIdLength - 1]); $namespace = strtolower($classIdParts[0]); for ($i = 1; $i < $classIdLength - 1; $i++) { $namespace .= '/' . $classIdParts[$i]; } if (file_exists(dirname(__FILE__)) . '/' . $namespace . '/' . $className . '.class.php') { include $namespace . '/' . $className . '.class.php'; } } spl_autoload_register('autoload'); 

You can then store this script and include it whichever script you use the use keyword.

Sign up to request clarification or add additional context in comments.

2 Comments

The use keyword doesn't actually do anything, so what actually it does? why is it exist?
As I understand it, Namespaces exist just to define scoping rules for a file or group of files. These files can contain functions, classes, etc. They don't actually do anything. Defining a namespace won't actually find that file and include it, that you have to do yourself. What it does is define a scope for the file thats already been include it.
2

You can also use autoloading with composer (App and src/ would be different for you though):

{ "require": { }, "autoload": { "psr-0": { "App": "src/" } } } 

You might also have to run composer.phar dumpautoload in the console.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.