"Fatal error: Argument 1 must not be null in ....."
Which is not strictly correct, but it got me thinking: maybe the guy that wrote the message was thinking ahead of a time when passing null for a classhinted arg would be allowed?
I don't see the big deal in being allowed to set default values on typehinted args - from my point of view this would have the result that objects or the given class or the default value is allowed and nothing else. I also don't see it as PHP's responsibility to stop me writing code which experienced programmers would balk at e.g.
myFunc(MyObj $o = 'fooBar')
In practice it makes no difference what the default value is or even if it is a value at all because the usefulness of the default is being able to check whether the object was passed or not (while letting the engine do the classhint check)
I am no PHP hero, but I'm not a noob either - and I think the argument that syntax would be confusing for beginners is a little silly - I remember trying to figure out the basics of OO, that was a nightmare in its own right. the point being the entry level to this functionality is way above what a beginner is capable of - if they can figure out how to do basic OO [in PHP] then they will be able to figure out using classhinted args with optional default values
kind-regards-hoping-that-some-form-of-defaults-on-classhinted-args-will-happen!
Jochem
Christian Schneider wrote:
Andi Gutmans wrote:
So one suggestion (also made a few months ago) was that the following: function method(MyClass $obj = NULL); would mark $obj as allowing NULL parameter.
The only restrictions I see is that it makes the parameter optional as well (something I don't consider a problem as it is probably desirable in most cases anyway to be able to leave out an explicit null) and that it only works if no mandatory parameter follows, e.g.
function method(MyClass $obj = null, $mandatory);
is not possible. Not a real problem either IMHO.
I still like this option more than anything else proposed here because it doesn't add new syntax and is simple to understand ("I can leave the parameter out then it will be null or I can pass null as I could also have left it out instead, otherwise I need to pass an object of MyClass").
- Chris
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php