Skip to content

is_callable() does actually work for constructors in context #1873

@JakeQZ

Description

@JakeQZ

https://www.php.net/manual/en/function.is-callable.php says

is_callable() reports constructors as not being callable.

This is not true. At least not when called from a context where the constructor is callable, e.g. a child class constructor.

    <?php
      class ParentWithConstructor
      {
        public function __construct()
        {
          ?>
            ParentWithConstructor::__construct() was called<br/>
          <?php
        }
      }
      class ParentWithoutConstructor
      {
      }
      class ChildOfParentWithConstructor extends ParentWithConstructor
      {
        public function __construct()
        {
          $isCallable = \is_callable([parent::class, '__construct']);
          ?>
            <?=static::class?>::__construct() was called<br/>
            $isCallable = <?=$isCallable?><br />
          <?php
          if ($isCallable) {
            parent::__construct();
          }
        }
      }
      class ChildOfParentWithoutConstructor extends ParentWithoutConstructor
      {
        public function __construct()
        {
          $isCallable = \is_callable([parent::class, '__construct']);
          ?>
            <?=static::class?>::__construct() was called<br/>
            $isCallable = <?=$isCallable?><br />
          <?php
          if ($isCallable) {
            parent::__construct();
          }
        }
      }
    ?>
    <p>
      <?php
        new ChildOfParentWithConstructor();
      ?>
    </p>
    <p>
      <?php
        new ChildOfParentWithoutConstructor();
      ?>
    </p>
    <p>
      PHP version <?=PHP_VERSION?>
    </p>

The output was

ChildOfParentWithConstructor::__construct() was called
$isCallable = 1
ParentWithConstructor::__construct() was called

ChildOfParentWithoutConstructor::__construct() was called
$isCallable =

PHP version 7.3.11

This needs to be clarified, rather than having a blanket statement saying it does not work, when in fact it does in the right situations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugDocumentation contains incorrect information

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions