36

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(); 
4
  • 1
    php.net/manual/en/function.include.php Commented Jan 3, 2010 at 11:21
  • 1
    I fixed your code sample - note that you can just type the code as it is instead of using HTML entities. Commented Jan 3, 2010 at 11:22
  • 1
    I don't understand how this question can be marked off topic ? It is a clear coding question, it contains code and the question is very easy to understand and to answer. Commented Sep 18, 2014 at 21:19
  • stackoverflow.com/a/14350866/6521116 Commented May 11, 2017 at 7:02

6 Answers 6

40

Your code should be something like

require_once('class.twitter.php'); $t = new twitter; $t->username = 'user'; $t->password = 'password'; $data = $t->publicTimeline(); 
Sign up to request clarification or add additional context in comments.

Comments

19

Include a class example with the use keyword from Command Line Interface:

PHP Namespaces don't work on the commandline unless you also include or require the php file. When the php file is sitting in the webspace where it is interpreted by the php daemon then you don't need the require line. All you need is the 'use' line.

  1. Create a new directory /home/el/bin

  2. Make a new file called namespace_example.php and put this code in there:

    <?php require '/home/el/bin/mylib.php'; use foobarwhatever\dingdong\penguinclass; $mypenguin = new penguinclass(); echo $mypenguin->msg(); ?> 
  3. Make another file called mylib.php and put this code in there:

    <?php namespace foobarwhatever\dingdong; class penguinclass { public function msg() { return "It's a beautiful day chris, come out and play! " . "NO! *SLAM!* taka taka taka taka."; } } ?> 
  4. Run it from commandline like this:

    el@apollo:~/bin$ php namespace_example.php 
  5. Which prints:

    It's a beautiful day chris, come out and play! NO! *SLAM!* taka taka taka taka 

See notes on this in the comments here: http://php.net/manual/en/language.namespaces.importing.php

Comments

15

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

Comments

6

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

3 Comments

A code example would be nice.
@PeterMortensen You can see examples here: php.net/manual/en/language.oop5.autoload.php One thing that php does really well is documentation
@AntonioCS If you look at the sheer mass of often times very useful and even crucial comments in the PHP docs you might want to ponder over the quality of documentation PHP delivers.
4
  1. require('/yourpath/yourphp.php');

    http://php.net/manual/en/function.require.php

  2. require_once('/yourpath/yourphp.php');

    http://php.net/manual/en/function.require-once.php

  3. include '/yourpath/yourphp.php';

    http://www.php.net/manual/en/function.include.php

  4. use \Yourapp\Yourname

    http://php.net/manual/fa/language.namespaces.importing.php

Notes:

Avoid using require_once because it is slow: Why is require_once so bad to use?

Comments

1

Check: http://www.php.net/require and http://www.php.net/include

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.