All articles

Build reusable PHP components

Can You Declare a Named Class Inside Another PHP Class?

See why a named class cannot be declared inside another PHP class, then use constructor injection and compare namespaces and anonymous classes.

Editorial illustration for Can You Declare a Named Class Inside Another PHP Class?

No. PHP does not support declaring an ordinary named class inside another class. If your goal is to keep a helper close to the code that uses it, declare the helper as its own class and give the outer class an instance. That gives you the working relationship you want without relying on nested-class syntax. It also makes an important distinction clear: an object can be held by a class, but the object's class declaration does not move inside it.

The named nested declaration is invalid PHP

The following is invalid PHP. It shows the tempting syntax, not a technique to copy into a working program:

<?php
class GreetingService
{
    class LabelFormatter
    {
    }
}

The problem is the class LabelFormatter declaration inside GreetingService. PHP's native syntax does not support that direct nesting, as this discussion of inner classes notes.

You may find a nested-classes RFC while searching for syntax. Its wording is explicit: it proposes support for nested classes. The access modifiers and virtual class namespace it describes belong to that proposal, not to the working syntax in the example below. A proposed language feature is not a reason to write a named class declaration inside another class.

Pass a separately declared helper into the class that uses it

Here is a self-contained example. LabelFormatter declares the helper type. GreetingService receives a LabelFormatter object through its constructor, stores it, and calls one of its methods:

<?php
class LabelFormatter
{
    public function format($name)
    {
        return 'Hello, ' . $name . '!';
    }
}

class GreetingService
{
    private $formatter;

    public function __construct(LabelFormatter $formatter)
    {
        $this->formatter = $formatter;
    }

    public function greet($name)
    {
        return $this->formatter->format($name);
    }
}

$service = new GreetingService(new LabelFormatter());
echo $service->greet('Ada'), "\n";

The specified output is:

Hello, Ada!

Save the runnable example as greeting.php if you want to check its syntax with php -l greeting.php, then run the file to check the output. The invalid snippet above is deliberately separate and should not be included in that file. The example's output follows from passing Ada to greet(), forwarding it to format(), and printing the returned string.

Notice where each operation happens. class LabelFormatter declares a class. new LabelFormatter() creates an object of that class. Assigning that object to $this->formatter lets GreetingService use it; it does not make LabelFormatter a nested declaration. The example calls a public method on the helper. Composition is an established alternative when classes need to work together without simulated nesting (discussion).

When a namespace or anonymous class is closer to what you need

If your concern is grouping related class names, a namespace can do that. It does not give one class nested-class visibility into another class's members, so it is not a substitute for privileged inner-class access. Keep using ordinary method calls or explicitly passed values for that relationship (source).

If the helper is a simple, one-off object rather than a named type you intend to reuse, an anonymous class may fit. Anonymous classes were introduced in PHP 7.0, and the PHP manual describes them as useful for one-off objects. Writing one inline within a class does not automatically expose that class's private or protected members to it. Pass a needed outer value to the anonymous object's constructor instead (discussion). For a named helper like LabelFormatter, keep the separate declaration and pass its object where it is needed.

← All articles