Can I instantiate a PHP class inside another class?

Can I instantiate a PHP class inside another class?

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

Stefan Bogdanescu

Founder & Senior Architect

Can I instantiate a PHP class inside another class?

Yes, you can absolutely instantiate a PHP class inside another class. There is no language restriction preventing a class from creating an instance of itself or another class within its methods or constructor. However, the architectural implications of doing so often dictate whether it is the right approach for your specific application.

The core issue usually isn't about capability, but about coupling and lifecycle management. When you instantiate a dependency directly inside a class using new, you are creating what is known as tight coupling. In your example, if some instantiates db, the some class now knows specifically that it needs a database connection and that that connection must be of type db. This creates a hard dependency. If you later decide to switch from db to an ORM like Eloquent, or if you need to inject a mock object for testing, you are forced to modify the internal code of some every time.

The alternative approach, which your source material hints at with "pass it in through the constructor," is Dependency Injection (DI). Instead of creating the instance internally, the class receives an already-instantiated object via its constructor argument. This shifts the responsibility of creation to the outside world—often a container or a bootstrap script that knows the application's full lifecycle and configuration.

Consider how a typical database interaction should look:

class Some {
    private $db;

    // The dependency is injected here, not created here
    public function __construct($db) {
        $this->db = $db;
    }

    public function getData() {
        return $this->db->query('SELECT * FROM users');
    }
}

In this pattern, the Some class remains agnostic to how the database connection was established. It doesn't care if the connection is hardcoded in a config file, pulled from an environment variable, or managed by a service locator. This flexibility is crucial for unit testing. If you need to test the logic inside Some, you can pass in a mock object that implements the same interface as db without needing a real database running.

Your specific code snippet shows a scenario where the class uses include and new directly within logic flow:

class Some {
    public function fetchData() {
        if (/* some condition */) {
            include SITE_ROOT . 'applicatie/db.class.php';
            $db = new db();
            
            // Use $db here
        }
    }
}

While this works technically, it introduces several pitfalls. First, dynamically including files inside methods is generally discouraged because it makes the code harder to track and can lead to namespace collisions or autoloading issues. Modern PHP projects almost universally rely on Composer's autoload mechanism rather than manual include statements within business logic. Second, instantiating a resource like a database connection directly inside a method often leads to resource leaks if that method is called repeatedly without closing the connection properly. Database connections are expensive resources; pooling them or managing their lifecycle at the application level (via a container) is far more efficient.

If you are working within the Laravel ecosystem, this question becomes even more relevant due to its built-in Dependency Injection container. In Laravel, you rarely instantiate classes manually inside methods. Instead, you rely on service providers to register bindings and use the resolve() method or constructor injection to ensure dependencies are handled correctly. For instance, if you need a database connection inside a class, you would typically type-hint the DB facade or an interface in the constructor:

class Some {
    protected $db;

    public function __construct(\Illuminate\Database\DatabaseManager $db) {
        $this->db = $db;
    }
}

This approach ensures that the database connection is managed by the framework's underlying service container, which handles closing connections and managing transaction states automatically.

Ultimately, instantiating a class inside another is permitted by PHP syntax, but it often signals an opportunity to refactor for better testability, loose coupling, and resource management.

Our network