IRC-Bot icon indicating copy to clipboard operation
IRC-Bot copied to clipboard

Including a class

Open ghost opened this issue 11 years ago • 10 comments

Hey, I'm trying to add twitter functionality, but I'm having real trouble with autoloader.php trying to load my class or something?

PHP Fatal error: Uncaught exception 'Exception' with message 'File: "/home/steve/Desktop/bot/Classes/TwitterAPIExchange.php" not found.' in /home/steve/Desktop/bot/Classes/Autoloader.php:40

How can I do this?

ghost avatar Feb 16 '14 14:02 ghost

Classes need the correct namespace at the top of the file, otherwise the auto-loader will be looking in the wrong place.

Where is this current class defined and what namespace have you given it?

As an example If it's a command the namespace should be "Command" and the file should be contained inside the Classes/Command/ directory.

ElvenSpellmaker avatar Feb 16 '14 15:02 ElvenSpellmaker

Hey,

I'm basically trying to implement https://github.com/J7mbo/twitter-api-php into the project in the form of a !twitter command. I'm just unsure how to include the required php (TwitterAPIExchange.php) file for it.

ghost avatar Feb 16 '14 15:02 ghost

OK, where have you put the file in the directory structure?

ElvenSpellmaker avatar Feb 16 '14 16:02 ElvenSpellmaker

Everywhere. I either get a duplicate class error or an autoloader error. Where should I put it?

ghost avatar Feb 16 '14 16:02 ghost

I think you might be misunderstanding me. Where have you put the file "TwitterAPIExhange.php"?

If you put it in the same folder as the Autoloader.php and then when you want to use it call "new \TwitterAPIExchange($settings);".

The auto-loader will then kick in on the first load and load the file once.

ElvenSpellmaker avatar Feb 16 '14 16:02 ElvenSpellmaker

The point of an auto-loader is to never have to use require/require_once/include or include_once yourself which tidies up the code and also avoids the multiply defined classes problems.

ElvenSpellmaker avatar Feb 16 '14 16:02 ElvenSpellmaker

Oh, excellent. Thanks!

ghost avatar Feb 16 '14 17:02 ghost

No problem. =)

I assume your issue is now resolved?

ElvenSpellmaker avatar Feb 16 '14 20:02 ElvenSpellmaker

Hey, I'd like to use 'simple_html_dom.php' and it's not a class. I'm not having much luck guessing where to put him. Could you spoon-feed me? :)

ghost avatar Sep 23 '14 16:09 ghost

Do you want to use that for a command?

NanoSector avatar Jan 03 '15 19:01 NanoSector