php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46899 Typed function arguments should allow nulls
Submitted: 2008-12-18 13:33 UTC Modified: 2012-06-05 23:15 UTC
From: zyss at mail dot zp dot ua Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.8 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: zyss at mail dot zp dot ua
New email:
PHP Version: OS:

 

 [2008-12-18 13:33 UTC] zyss at mail dot zp dot ua
Description:
------------
Currently class type can be specified as function argument type, but it is frequently required to pass null instead of object reference when there is a default argument value set and it is null. In the following example constructor's $parent argument can be null for the top-level objects, but current PHP version doesn't allow it to be null forcing to remove type declaration that is very undesirable:

  class ExElement extends Exception { };

  class Element {
    protected /* Document */ $document; // each element references document for fast access
    protected /* Element */ $parent;

    function __construct(Document $document, Element $parent = null) /* throws ExElement */ {
      $this->document = $document; // is still checked by PHP to be valid Document object reference
      if ($parent && ($parent->getDocument() != $document))
        throw new ExElement("Parent's document doesn't match Element constructor's argument", 1);
      $this->parent = $parent;
    }

    function getDocument() {
      return $this->document;
    }
  }


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-18 13:41 UTC] zyss at mail dot zp dot ua
Example in more readable form:

  class ExElement extends Exception { };

  class Element {
    // each element references document for fast access
    protected /* Document */ $document;
    protected /* Element */ $parent;

    function __construct(
      Document $document,
      Element $parent = null) /* throws ExElement */
    {
      // is still checked to be valid Document object reference
      $this->document = $document;

      if ($parent && ($parent->getDocument() != $document))
        throw new ExElement("Parent's document doesn't match " .
          "Element constructor's argument", 1);
      $this->parent = $parent;
    }

    function getDocument() {
      return $this->document;
    }
  }
 [2010-12-31 23:59 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: Irrelevant +Operating System: *
 [2012-06-05 23:15 UTC] nikic@php.net
Closing as this is already implemented. At least in PHP 5.3 and higher any typehint with a null default value can also be called explicitly with null.

See http://codepad.viper-7.com/CHcVzX for a demo.
 [2012-06-05 23:15 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 05:01:30 2024 UTC