0

I think this is very simple for many of you, but in the moment I got stuck with this. I have the following part of code:

header.php

include "facebook/autoload.php";

use Facebook\FacebookRedirectLoginHelper;

test.php

include "header.php";
$helper = new FacebookRedirectLoginHelper($redirect_url);

Why do I always get this error:

Fatal error: Class 'FacebookRedirectLoginHelper' not found in test.php on line

I thought when I include a PHP file, classes can also be used. But in this case not, why? I think I do not understand how this autoload and use works, so I would be happy for some explanation.

Qiu
  • 5,651
  • 10
  • 49
  • 56
Ruven JR. Maerson
  • 309
  • 1
  • 6
  • 16
  • 1
    Classes yeas, but I think that does not apply for the namespaces. If you could copy the `use` line from header in the test.php and check again. – DaGhostman Dimitrov May 26 '15 at 17:32
  • Yeah this works! I must add this use line in every php file then???? – Ruven JR. Maerson May 26 '15 at 17:34
  • 1
    Yes, that tells the script that the class `FacebookRedirectLoginHelper` is in that namespace and not in the global one. Alternatively you could use `$helper = new \Facebook\FacebookRedirectLoginHelper($redirect_url);` which is perfectly valid on its own, but you will end up wit too long lines in some cases. – DaGhostman Dimitrov May 26 '15 at 17:36
  • but why this use command does not inherit the class namespace in the test.php? I do not understand that. Where is the advantage here? – Ruven JR. Maerson May 26 '15 at 17:43
  • see my answer for additional info. If I missed something let me know so I could update/clarify/improve – DaGhostman Dimitrov May 26 '15 at 18:00

1 Answers1

1

PHP does not inherit the namespaces nor the use statements of included/required files. This is intentional as otherwise if you include 2 files using a class aliased the same way you will get errors and you might not need all those classes in firs place.

If a class requires a namespace it has to have use statement defined with the full namespace to the particular class they need. Except in the cases where there might be aliasing. For example if you have:

// file1.php
use \My\Cool\LogWriter as Writer;

and

// file2.php
use \My\Cool\FileWriter as Writer;

Now both classes are accessible as Writer.

// test.php
require 'file1.php';
require 'file2.php';

In which case if you don't declare which class from which space you want this will give nasty error that class Writer is defined, which is true, but it is also true that the two classes are 2 separate ones.

For more information on namespaces in PHP5 see (http://php.net/manual/en/language.namespaces.php).

As a side note:

  • Every file, if not namespace declaration is provided is considered in the global namespace.
  • If a use is without leading slash the namespace might be considered relative to the current. (unsure but I think it depends on the autoloader?) (Reference here: https://stackoverflow.com/a/4879615/1747193)
Community
  • 1
  • 1
DaGhostman Dimitrov
  • 1,608
  • 20
  • 44