How to include a class in PHP

How to include a class in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to include a class in PHP

In PHP, loading external code into your current execution context is a fundamental mechanism for organizing application logic. When you have a standalone index.php and need to integrate functionality defined in class.twitter.php, you are essentially asking the interpreter to read that file's contents and register any contained classes within the current namespace scope. The specific method you choose depends heavily on whether that target file is needed only once or if it must be accessible from multiple locations within your project.

Using require for Critical Dependencies

The most direct answer to including a class when you are building an application like a Twitter client wrapper is using the require statement. In your index.php, you would place the following code:

<?php
require 'class.twitter.php';

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

try {
    $data = $t->publicTimeline();
} catch (Exception $e) {
    // Handle errors
}

Here, require instructs the PHP engine to load the file immediately. If class.twitter.php does not exist or is unreachable, execution halts instantly with a fatal error and a warning message sent to the output buffer. This behavior enforces a strict dependency contract: if your application cannot run without this specific class, require is the correct tool because it guarantees the failure of the script rather than silently ignoring a missing component.

Using include for Optional Logic

Conversely, if you are creating a modular library where class.twitter.php might be optional or loaded conditionally, you would use include. The syntax changes slightly:

<?php
if (file_exists('class.twitter.php')) {
    include 'class.twitter.php';
}

With include, if the file is missing, PHP emits a warning but continues executing the rest of the script. This trade-off is rarely recommended for core class definitions unless you are implementing a highly specific fallback mechanism. For instance, in the example above, trying to instantiate a new twitter() object without the class being loaded would result in a fatal error later on anyway. Therefore, for defining classes that are expected to be used immediately, require is generally preferred over include.

Namespace Considerations and Autoloading

It is important to note that simply loading a file does not automatically resolve namespace collisions if your project grows larger. In the provided example, the class is named twitter. If you have other dependencies using the same name or different namespaces, manual inclusion can lead to conflicts or require explicit use statements.

Modern PHP applications often avoid manual include or require statements for classes entirely by implementing an autoloader. An autoloader intercepts requests for missing classes and attempts to locate the corresponding file based on the class name. However, if you are working with a legacy codebase or a simple script where you specifically need to execute logic in class.twitter.php before doing anything else, manual inclusion remains a valid and necessary technique.

Security Implications of Manual Inclusion

When manually including files like class.twitter.php, ensure that the path is resolved relative to your current working directory or configured correctly within your project structure. Avoid using require with user-supplied input to determine which file to load, as this opens your application to Remote Code Execution vulnerabilities. Always validate that the target file exists and resides within a trusted directory before attempting to include it.

For developers looking to streamline class loading in larger frameworks or applications, platforms like https://laravelcompany.com offer robust solutions where dependency management is handled automatically, reducing the need for manual require statements in every single file. However, understanding the mechanics of include and require remains essential when debugging issues or working outside of such framework abstractions.

Our network