Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a class in PHP [closed]

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

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

$t = new twitter(); $t->username = 'user'; $t->password = 'password';  $data = $t->publicTimeline(); 
like image 562
CLiown Avatar asked Jan 03 '10 11:01

CLiown


People also ask

What is include() and require() function in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is class& object in PHP?

A class is a template for objects, and an object is an instance of class.


2 Answers

Your code should be something like

require_once('class.twitter.php');  $t = new twitter; $t->username = 'user'; $t->password = 'password';  $data = $t->publicTimeline(); 
like image 148
Mez Avatar answered Sep 17 '22 10:09

Mez


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

like image 24
richsage Avatar answered Sep 19 '22 10:09

richsage