|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-06-17 05:48 UTC] nospam0 at malkusch dot de
Description:
------------
Since RC3 I can't define optional parameters when I'm
using type hinting. Why is it no more allowed to use NULL.
Im used to do so from other OO Languages.
So if I have to live with no NULL and want to use class
Type hints, how can I use optional Paramters?
Reproduce code:
---------------
class Test {
public function test(Test $optinalTest = null) {
}
}
Expected result:
----------------
No Error, or a possibility to use class type hints *and*
optinal parameters. Perfect would be the old behavoir,
that I'm allowed to use NULL.
Actual result:
--------------
Fatal error: Argument 1 must not be null
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 09:00:01 2025 UTC |
> - Typhints are a shortcut for an 'instanceof' test. I'll never use instanceOf in any OO Language. It's sign for a bad design. > - what you probably want is > function bla($x) { > if (is_null($x)) { > // handle null > } else if ($x instanceof whatever) { > // handle instance > } else { > // handle error > } > } > > - if you look again you\'ll see that you are doing > *three different* things in your code. I don't want that. I want for example this: class Node { private $parent; private $children = array(); public function setParent(Node $node) { $this->parent = $node; } public function removeChild($childID) { $this->children[$childID]->setParent(NULL); unset($this->children[$childID]); } ... } Or I simply want to have the possibility to use optional Paramters and Type Hints: class Foo { public function setDate(Date $date = NULL) { if (is_null($date)) { $date = new Date(time()); } ... } > Typehints have a different usage! The please tell me what usage they have! I thaught they might bring PHP nearer to an strict language, but if I have to do without NULL or the possibility to use optional Parameters I can't use Type Hints strictly. By the way in Java, OCL (and C++ I think too), NULL can be passed as any Type.In any given language you can pass NULL only for pointers. If you never use instanceof in any language then you do not program type strict in any language. And once again passing NULL and passing an instance is different. In most cases allowing or even expecting this looks like bad design because you obviously wanted to have a two functions: function set(classType $val) { $this->val = $val; } function unset() { unset($this->val); } Anyway we know the problem and have different solutions for it. Chances are good we apply one of them for 5.1.