php / php/doc-en

is_callable() does actually work for constructors in context

Open
#1,873 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the is_callable() documentation page linked in the issue and review the statement about constructors against the supplied PHP example. Clarify the statement to account for callable context, then verify that the revised wording accurately reflects the demonstrated behavior and PHP documentation style.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.