views:

108

answers:

5

I have index.php and I want to include class.twitter.php inside it, how do I do this?

Hopefully then when I put the below code in index.php it will work.

$t = new twitter();
$t->username = 'user';
$t->password = 'password';

$data = $t->publicTimeline();
A: 

Have a look at require() and require_once().

These should do the job.

peter p
+1  A: 

Your code should be something like

require_once('class.twitter.php');

$t = new twitter;
$t->username = 'user';
$t->password = 'password';

$data = $t->publicTimeline();
Mez
+6  A: 

You can use either of the following:

include "class.twitter.php";

or

require "class.twitter.php";

Using require (or require_once if you want to ensure the class is only loaded once during execution) will cause a fatal error to be raised if the file doesn't exist, whereas include will only raise a warning. See http://php.net/require and http://php.net/include for more details

richsage
+2  A: 

I suggest you also take a look at __autoload.
This will clean up the code of requires and includes.

AntonioCS