2

I am attempting to call a class in a required file, but I am getting the error

PHP Fatal error:  Class 'SampleClass' not found in /home2/domain/public_html/website/v2/wp-content/plugins/myplugin.php on line 23

I can tell that the file is required correctly as I have echoed some text in it and it displays. I am using Wordpress - is there anything weird about Wordpress which would prevent this from working?

As soon as I copied the class into the original file (as opposed to including it from somewhere else) it works fine.

Any ideas? Can provide more info if needed, but I'm opposed to copying the code it here because it would be mostly irrelevant.

Sam
  • 6,616
  • 8
  • 35
  • 64
  • please try print_r(get_declared_classes()); before and after require() – fsw Jul 18 '13 at 08:39
  • There is exactly the same amount of classes (366) before and after the require, yet my echo "test" in the required file displays between them (where the require is) – Sam Jul 18 '13 at 08:43
  • Can we see the included file? –  Jul 18 '13 at 08:44
  • It is this http://sourceforge.net/projects/codebird-php.codebird.p/?source=dlp but I have put echo "test"; underneath the namespace declaration. – Sam Jul 18 '13 at 08:44
  • i think "namespace" might be the key to the mystery. do you know how they work? what namespace does included file operates on and what is the namespace of the calling file? how are you trying to access your class namespace? – fsw Jul 18 '13 at 08:47
  • I had a quick look at the PHP docs on namespaces; but I'm basically attempting to get authenticate with Twitter as per this tutorial http://kevin-deleon.com/2013/06/tutorial-display-recent-tweets-using-jquery-php-and-twitters-api/. With the new API my old JS stuff stopped working. I have copied + pasted the code there. Class GetTweets has been copied directly into the file I'm working with, as requiring it returned the same error as I am getting with CodeBird - making me think there's an issue with requiring files and calling classes from within. – Sam Jul 18 '13 at 08:50
  • 1
    I think @fsw is correct. You probaly need to prefix the class with the namespace when referencing it `Namespace\SampleClass;` or use the [Use](http://php.net/manual/en/language.namespaces.importing.php) function in PHP. – Hein Andre Grønnestad Jul 18 '13 at 08:51
  • Namespace of codebird.php is Codebird, whereas in the file calling the require it is blank. However, if you look into that link I posted in the previous comment, when the class is called the namespace is called (\Codebird\Codebird::method) – Sam Jul 18 '13 at 08:56

1 Answers1

0

To use that class you have to prefix the class name with the namespace for example:-

Instead of:

new Codebird;

You would write:

new Codebird\Codebird;

Mike Hancock
  • 230
  • 2
  • 8