AW: [PHP-DEV] Method overloading by method signature
| From: | Hans Moog | Date: | Mon, 15 Oct 2007 19:18:23 +0000 |
| Subject: | AW: [PHP-DEV] Method overloading by method signature | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-32791@lists.php.net to get a copy of this message | ||
When it would be:
==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}
function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}
function xpath(string $arg) {
return new DomXPath($this->loadXML($arg));
}
==
(since when method overloding by sigantures wer put into php, scalar types should be possible, too,
shouldn't they?)
I would prefer the second one because I understand the behaviour much faster than analyzing the if
switch (and this if switch is really simple to understand because there is only one command in every
block). The second reason for me to select the second version is, that I am able to write better
documentation for each behaviour (although, in this situation there isn't that much to be
commented).
-----Ursprüngliche Nachricht-----
Von: Timm Friebe [mailto:thekid@thekid.de]
Gesendet: Montag, 15. Oktober 2007 20:47
An: internals@lists.php.net
Betreff: Re: [PHP-DEV] Method overloading by method signature
Hi,
[...]
> Later we added type hints to help code readability.
Let me jump at this:
==
function xpath($arg) {
if ($arg instanceof DomDocument) {
return new DomXPath($arg);
} else if ($arg instanceof XmlTree) {
return new DomXPath($this->loadXML($arg->getSource()));
} else if (is_string($arg)) {
return new DomXPath($this->loadXML($arg));
} else {
throw new IllegalArgumentException('Unsupported argument type');
}
}
== vs. ==
function xpath(DomDocument $arg) {
return new DomXPath($arg);
}
function xpath(XmlTree $arg) {
return new DomXPath($this->loadXML($arg->getSource())));
}
function xpath($arg) { // Untyped = default
if (!is_string($arg)) {
throw new IllegalArgumentException('Unsupported argument type');
}
return new DomXPath($this->loadXML($arg));
}
==
If we consider the readability argument only: Which one of the above more
readable? You decide:)
- Timm
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php