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();
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.
A class is a template for objects, and an object is an instance of class.
Your code should be something like
require_once('class.twitter.php'); $t = new twitter; $t->username = 'user'; $t->password = 'password'; $data = $t->publicTimeline();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With