Edit report at https://bugs.php.net/bug.php?id=46899&edit=1

 ID:                 46899
 Updated by:         [email protected]
 Reported by:        zyss at mail dot zp dot ua
 Summary:            Typed function arguments should allow nulls
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        5.2.8
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2008-12-18 13:41:08] 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;
    }
  }

------------------------------------------------------------------------
[2008-12-18 13:33:55] 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;
    }
  }



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=46899&edit=1

Reply via email to