Skip to content

[BUG]: getDI() returns null but declares DiInterface (InjectionAwareInterface, AbstractInjectionAware and 5 classes) #17787

Todo

Description

@niden

Describe the bug

Phalcon\Di\AbstractInjectionAware::getDI() declares the return type Phalcon\Di\DiInterface (phalcon/Di/AbstractInjectionAware.zep:30), but it returns null when no container is set. Phalcon\Di\InjectionAwareInterface::getDI() declares the same return type (phalcon/Di/InjectionAwareInterface.zep:22).

Five other classes declare the same return type, and their container property can be null too:

Class File
Phalcon\Session\Bag phalcon/Session/Bag.zep:89
Phalcon\Mvc\Model\Manager phalcon/Mvc/Model/Manager.zep:1168
Phalcon\Mvc\Model\Query phalcon/Mvc/Model/Query.zep:546
Phalcon\Mvc\Model\Transaction\Manager phalcon/Mvc/Model/Transaction/Manager.zep:164
Phalcon\Mvc\View\Engine\Volt\Compiler phalcon/Mvc/View/Engine/Volt/Compiler.zep:2091
  • A release PHP build does not check the return type of internal functions. The caller gets null.
  • A PHP debug build (--enable-debug) checks the return type and stops with a fatal error.

Code in Phalcon already expects null. For example, Phalcon\Http\Request::resolveAuthorizationHeaders() calls getDI() and then checks container !== null.

The PHP implementation (phalcon/phalcon) already declares DiInterface | null for these 7 methods.

To Reproduce

PHP compiled with --enable-debug:

<?php

$request = new Phalcon\Http\Request();

var_dump($request->getDI());

Output:

Fatal error: Phalcon\Di\AbstractInjectionAware::getDI(): Return value must be of type Phalcon\Di\DiInterface, null returned

The test suite stops at the same error in tests/unit/Http/Request/GetHeadersTest.php:40 (Request::getHeaders() -> resolveAuthorizationHeaders() -> getDI()). With a release PHP build, the same script prints NULL.

Expected behavior

The declared return types agree with the returned values, the same as the PHP implementation.

Possible fix

Declare the return type <DiInterface> | null for the 7 methods:

public function getDI() -> <DiInterface> | null

The returned values do not change. A class that implements InjectionAwareInterface, or extends one of these classes, and declares the return type DiInterface stays compatible. Phalcon\Di\Injectable, Phalcon\Http\Response, Phalcon\Mvc\Model\Criteria, Phalcon\Mvc\Model\MetaData, Phalcon\Mvc\Model\Query\Builder and Phalcon\Tag keep DiInterface, the same as the PHP implementation.

Details

  • Phalcon version: 5.23.0 (5.0.x)
  • PHP Version: 8.6.0 with --enable-debug
  • Operating System: Debian (docker)
  • Installation type: Compiling from source

Activity

  1. added
    bug
    on Nov 24, 2026
  2. self-assigned this
    on Nov 24, 2026
  3. added
    5.0
    on Nov 24, 2026
  4. added theissue type on Nov 24, 2026
  5. added and removed
    5.0
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

7.0bugstatus: medium

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions