PHP: How to include a class


Question

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();
1
25
12/12/2013 4:49:42 PM

Accepted Answer

Your code should be something like

require_once('class.twitter.php');

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

$data = $t->publicTimeline();
30
1/3/2010 11:21:12 AM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon