|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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;
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 18:00:01 2025 UTC |
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; } }