0

I have two files, the first is Test.php which looks like this:

<?php
namespace My\Namespaces\Test;

class Test {
    const ALERT_EMAIL = "AlertEmail";
    const ALERT_SMS = "AlertSms";
    const ALERT_NOTIFICATION = "AlertNotification";
}  // class - EnumAlertType
?>

and another file try to use const from Test.php

<?php
use My\Namespaces\Test;
$a = Test::ALERT_SMS;
?>

but i still get the Class 'My\Namespaces\Test' not found error, i'm not sure if i'm using the namespace correctly. Thanks

user2810081
  • 589
  • 1
  • 8
  • 27
  • 3
    I assume that's a typo while opening the question in your second example (`Namespace` vs `Namespaces`), right? Anyway, have you configured autoloading(as I don't see any mentions of manual includes in your code) correctly? – Rangad Jul 21 '15 at 21:41
  • 2
    Maybe if you would `include()` the file instead of just `use` the class. – D4V1D Jul 21 '15 at 21:42
  • possible duplicate of [Autoload classes from different folders](http://stackoverflow.com/questions/5280347/autoload-classes-from-different-folders) – D4V1D Jul 21 '15 at 21:43
  • The full class name here would be `My\Namespaces\Test\Test`. To get at the constant you would have to do this: `My\Namespaces\Test\Test::ALERT_SMS`. Namespaces are just like folders on your hard disk, and any defined classes are like files. `My\Namespaces\Test` is the folder, and `Test` is the file. – Sverri M. Olsen Jul 21 '15 at 21:48
  • @SverriM.Olsen: Namespaces are just like folder up to the path to the class. Therefore `My\Namespaces\Test` represent a class, not a folder. [See the `use My\Full\Classname` in the PHP doc](http://php.net/manual/en/language.namespaces.importing.php). – D4V1D Jul 21 '15 at 21:56
  • @SverriM.Olsen Hi, I've tried that just now, but still get the error: PHP Fatal error: Class 'My\Namespaces\Test\Test' not found – user2810081 Jul 21 '15 at 21:57
  • Have you tried using `require_once();`? – D4V1D Jul 21 '15 at 22:05

1 Answers1

2

You need to differenciate two terms here: including and importing. The former is the add the code in the current script being executed and the latter is to use them easily in your code. Including litteraly copy paste the code to the current script so it can be used later on.

Thus, you need to include (require_once()) the code of the class Test into the file that will use that code. You can, indeed, import (use) it afterwards (especially if files are in separated folders). Therefore, you need to do:

<?php
require_once('Test.php'); // include the code
use My\Namespaces\Test; // import it if you want but not useful as the two file are in the same folder
$a = Test::ALERT_SMS; // access to class constants

You should start digging into the spl_autoloader_register() function.

D4V1D
  • 5,805
  • 3
  • 30
  • 65
  • Hi, I tried using require_once and it works, really thank you for that. but i'm still confused, when i use the symfony framework, i can import other classes directly without the require_once(), i'm not sure if symfony makes any difference.. – user2810081 Jul 21 '15 at 22:21
  • [Of course Symfony does make a difference](http://symfony.com/doc/current/components/class_loader/index.html) like ZF and, I'm pretty sure, any other framework. – D4V1D Jul 21 '15 at 22:22
  • Have you considered accepting this answer if it solved your problem? – D4V1D Jul 21 '15 at 22:45
  • 1
    "Importing" is confusing to use here; *aliasing* would be the correct term. – deceze Jun 22 '17 at 08:30