Type hints with null default values

php.internals

Cristiano Duarte

21 years ago
Hi @ll, Now that 5.0.3 is out, maybe it's time to bring back that old problem left in May: type hints and null default values. This feature was dropped since no one could come with a reasonable solution. So, for now, there is no (engine) way to have a parameter that can only be of some class or null. Am I right ? The use of null (I know it's not a value it's a type...) is justified because we don't have a "value" to represent a NULL OBJECT(or NO OBJECT). IMO, this is a necessary feature (maybe others thing this way too) and that's why I brought it back. Cristiano Duarte

Georg Richter

21 years ago
Am Sa, den 16.10.2004 schrieb Cristiano Duarte um 21:13:
> Hi @ll, > > Now that 5.0.3 is out
Doohh 5.0.3? I didn't see any announcement and I planned to commit several stuff before :( /Georg
-- Georg Richter, Senior Software Developer MySQL AB, www.mysql.com See you at MySQL ComCon Europe November 8-10! www.mysqlcomconeurope.com

Cristiano Duarte

21 years ago
Georg Richter wrote:
> Am Sa, den 16.10.2004 schrieb Cristiano Duarte um 21:13: >> Hi @ll, >> >> Now that 5.0.3 is out > > Doohh 5.0.3? I didn't see any announcement and I planned to commit > several stuff before :(
Sorry Georg. I meant: Now that 5.0.2 is out... Cristiano Duarte

Markus Fischer

21 years ago
Cristiano Duarte wrote:
> Now that 5.0.3 is out, maybe it's time to bring back that > old problem left in May: type hints and null default values. > This feature was dropped since no one could come with a > reasonable solution. > So, for now, there is no (engine) way to have a parameter that > can only be of some class or null. Am I right ? > The use of null (I know it's not a value it's a type...) is > justified because we don't have a "value" to represent > a NULL OBJECT(or NO OBJECT). > IMO, this is a necessary feature (maybe others thing this way > too) and that's why I brought it back.
I'm also realing waiting for this implemented. Since I've started to use the new features of v5, I've had to deal with parameter checks in exactly this way. If it's not class/interface instanceof type x, throw an exception. I just hope, if it gets implemented, it's done via exceptions too. - Markus

Robert Silva

21 years ago
I think Marcus B's final comments in the original discussion are right on track. Stab: I voted for NULL type hinted arguments before I voted against them. The main thing that changed my mind, is that in PHP there is no such thing as a NULL object. I came to the conclusion that PHP wasn't broken, my programming style/code was. My opinion is that they should stay as they are now although personally. Now with that said, I do think there needs to be a method to allow a type hinted parameter to be optional (but not necessarily null). class a { function b(Obj c=null) {}; } $a->b(null) or $a->b($anything_not_an_Obj) would be an error. $a->b() would be valid. function Format(ZIFormatter $formatter=null) { if (isset($formatter)) return $formatter->Format($this); else return $this; } Now the argument can be made to not type hint $formatter to begin with, but I don't want to have to check that it isnt null and instanceof ZIFormatter interface. I just want to know if it was passed, that I can use it as a ZIFormatter object. Bob Silva -----Original Message----- From: Cristiano Duarte [mailto:cunha17@uol.com.br] Sent: Saturday, October 16, 2004 12:14 PM To: internals@lists.php.net Subject: [PHP-DEV] Type hints with null default values Hi @ll, Now that 5.0.3 is out, maybe it's time to bring back that old problem left in May: type hints and null default values. This feature was dropped since no one could come with a reasonable solution. So, for now, there is no (engine) way to have a parameter that can only be of some class or null. Am I right ? The use of null (I know it's not a value it's a type...) is justified because we don't have a "value" to represent a NULL OBJECT(or NO OBJECT). IMO, this is a necessary feature (maybe others thing this way too) and that's why I brought it back. Cristiano Duarte
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Cristiano Duarte

21 years ago
Robert Silva wrote:
> I think Marcus B's final comments in the original discussion are right on > track. > > Stab: I voted for NULL type hinted arguments before I voted against them. > > The main thing that changed my mind, is that in PHP there is no such thing > as a NULL object. I came to the conclusion that PHP wasn't broken, my > programming style/code was. > > My opinion is that they should stay as they are now although personally. > > Now with that said, I do think there needs to be a method to allow a type > hinted parameter to be optional (but not necessarily null).
That's what I'm asking for. :-) I'm not asking for null objects, just an optional parameter that must be an object or a "null" reference. All OO languages I've ever programmed had this kind of reference(Java, C++, Delphi, etc.) Cristiano Duarte

Robert Silva

21 years ago
PHP doesn't have a concept of a "null" reference though. So to say that every other language allows it is not a valid argument. The other languages allow it because the language supports it. And that seems to be what the arguments against it centered around. I think a compromise would be to allow the argument to be optional but still enforce the type hint. What this doesn't address fully is multiple optional type hinted arguments. function A(ZObj $arg2=null, ZObj $arg3=null) {} How would you pass $arg3 if you didn't have anything to pass for $arg2. Maybe that's just an inherent restriction of the language...to pass $arg3, you have to pass a ZObj as $arg2. Someone else suggested adding null references to the language: ZObj $obj = null; But I don't think that is the right solution either not to mention the nightmare of actually implementing it. Bob -----Original Message----- From: Cristiano Duarte [mailto:cunha17@uol.com.br] Sent: Saturday, October 16, 2004 3:01 PM To: internals@lists.php.net Subject: RE: [PHP-DEV] Type hints with null default values Robert Silva wrote:
> I think Marcus B's final comments in the original discussion are right on > track. > > Stab: I voted for NULL type hinted arguments before I voted against them. > > The main thing that changed my mind, is that in PHP there is no such thing > as a NULL object. I came to the conclusion that PHP wasn't broken, my > programming style/code was. > > My opinion is that they should stay as they are now although personally. > > Now with that said, I do think there needs to be a method to allow a type > hinted parameter to be optional (but not necessarily null).
That's what I'm asking for. :-) I'm not asking for null objects, just an optional parameter that must be an object or a "null" reference. All OO languages I've ever programmed had this kind of reference(Java, C++, Delphi, etc.) Cristiano Duarte
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Jevon Wright

21 years ago
Alternatively, you could try to support polymorphism in classes :o) Then you wouldn't need null references... class X { function a(); function a(MyObj $b); } Jevon Wright ----- Original Message ----- From: "Robert Silva" <junkmail@bobsilva.com> To: <internals@lists.php.net> Sent: Sunday, October 17, 2004 11:24 AM Subject: RE: [PHP-DEV] Type hints with null default values
> PHP doesn't have a concept of a "null" reference though. So to say that > every other language allows it is not a valid argument. The other
languages
> allow it because the language supports it. And that seems to be what the > arguments against it centered around. > > I think a compromise would be to allow the argument to be optional but
still
> enforce the type hint. What this doesn't address fully is multiple
optional
> type hinted arguments. > > function A(ZObj $arg2=null, ZObj $arg3=null) {} > > How would you pass $arg3 if you didn't have anything to pass for $arg2. > Maybe that's just an inherent restriction of the language...to pass $arg3, > you have to pass a ZObj as $arg2. > > Someone else suggested adding null references to the language: > > ZObj $obj = null; > > But I don't think that is the right solution either not to mention the > nightmare of actually implementing it. > > > Bob > > -----Original Message----- > From: Cristiano Duarte [mailto:cunha17@uol.com.br] > Sent: Saturday, October 16, 2004 3:01 PM > To: internals@lists.php.net > Subject: RE: [PHP-DEV] Type hints with null default values > > Robert Silva wrote: > > > I think Marcus B's final comments in the original discussion are right
on
> > track. > > > > Stab: I voted for NULL type hinted arguments before I voted against
them.
> > > > The main thing that changed my mind, is that in PHP there is no such
thing
> > as a NULL object. I came to the conclusion that PHP wasn't broken, my > > programming style/code was. > > > > My opinion is that they should stay as they are now although personally. > > > > Now with that said, I do think there needs to be a method to allow a
type
> > hinted parameter to be optional (but not necessarily null). > That's what I'm asking for. :-) > I'm not asking for null objects, just an optional parameter that must be
an
> object or a "null" reference. > All OO languages I've ever programmed had this kind of reference(Java,
C++,

Cristiano Duarte

21 years ago
Robert Silva wrote:
> PHP doesn't have a concept of a "null" reference though. So to say that > every other language allows it is not a valid argument. The other > languages allow it because the language supports it. And that seems to be > what the arguments against it centered around. > > I think a compromise would be to allow the argument to be optional but > still enforce the type hint. What this doesn't address fully is multiple > optional type hinted arguments. > > function A(ZObj $arg2=null, ZObj $arg3=null) {} > > How would you pass $arg3 if you didn't have anything to pass for $arg2. > Maybe that's just an inherent restriction of the language...to pass $arg3, > you have to pass a ZObj as $arg2.
Since PHP doesn't support explicit method overloading, we should support some form of telling the function that $arg2 wasn't passed. This problem can be solved with named parameters instead os positional ones, but someone suggested it a long ago and, at that moment, it would not be feasible.
> > Someone else suggested adding null references to the language: > > ZObj $obj = null; > > But I don't think that is the right solution either not to mention the > nightmare of actually implementing it.
It was supported before(until May), isn't it ? IMHO, what we have now is "not null typehints" so what's the matter if we accept "not null typehints" when we have this construction: function A(ZObj $arg2, ZObj $arg3) {} and "null typehints" with this: function A(ZObj $arg2=null, ZObj $arg3=null) {} as we supported before(except that with the first construction nulls are not allowed). Maybe I'm "blind" but I can't see the problem that Marcus pointed anymore. If I can remember, he wants a way to enforce typehints not allowing nulls: construction-1 does it. I really need optional typehints and construction-2 supports it. IMHO, everybody would be happy. Any comments ? Regards, Cristiano Duarte

Robert Silva

21 years ago
Re-reading the previous discussion, it seems like the functionality that people wanted was to have an optional typed argument. In converting the .NET Framework library over to PHP, there are many instances where having optional typed params would have been handy. The Framework makes heavy use of interfaces, usually implemented as overloaded methods. When implementing it in PHP I had to make use of func_num_args() and func_get_args() etc.. to simulate the overloading. As I convert the classes over to an extension, I am able to define the method arguments as optional and be of a certain type if they are passed. It would be nice to have similar functionality in userland. The quick solution is to have =null override the zend_verify_arg_type is_null check, but then this lets users pass null as a value and you lose your type hint. The solution I proposed makes use of the current method of making parameters optional, BUT it would still be a runtime error to pass NULL to the function. 1. It doesn't add new keywords to the language 2. There is no "magic", it would be a runtime error to pass null. 3. It does change the "expected" functionality of $obj=default, but it is limited to the scope of type hints. 4. Doesn't affect BC (since they can't use a default now) 5. It allows optional typed parameters 6. You still have to check for null 7. Downside - you cant pass optional params after the first one. I patched my code base to see if it would work and it does but it's not an elegant solution. Basically, inside the vm_handler for ZEND_RECV_INIT, if no parameter was passed and null has been specified as the default, then it calls an extended zend_verify_arg_type_ex and passes an allow_null param to override the IS_NULL check. So now, the code below works as spec'd out above. Typed optional params that cannot be null. <? class Object { public function Method(Object $o=null, Object $p=null) { if (!is_null($o)) echo 'Got Object'.PHP_EOL; else echo 'Empty Param'.PHP_EOL; } } $a = new Object(); $a->Method($a); $a->Method($a, $a); $a->Method(); $a->Method(null); <-- Error, null not allowed $a->Method(null, $a); <-- Can't get to second argument since null can not be passed ?> #>php -n -f test.php Got Object Got Object Empty Param Fatal error: Argument 1 must not be null in E:\php5\php5cvs\php5\Debug_TS\test.php on line 5 Bob Silva -----Original Message----- From: Cristiano Duarte [mailto:cunha17@uol.com.br] Sent: Saturday, October 16, 2004 4:02 PM To: internals@lists.php.net Subject: RE: [PHP-DEV] Type hints with null default values Robert Silva wrote:
> PHP doesn't have a concept of a "null" reference though. So to say that > every other language allows it is not a valid argument. The other > languages allow it because the language supports it. And that seems to be > what the arguments against it centered around. > > I think a compromise would be to allow the argument to be optional but > still enforce the type hint. What this doesn't address fully is multiple > optional type hinted arguments. > > function A(ZObj $arg2=null, ZObj $arg3=null) {} > > How would you pass $arg3 if you didn't have anything to pass for $arg2. > Maybe that's just an inherent restriction of the language...to pass $arg3, > you have to pass a ZObj as $arg2.
Since PHP doesn't support explicit method overloading, we should support some form of telling the function that $arg2 wasn't passed. This problem can be solved with named parameters instead os positional ones, but someone suggested it a long ago and, at that moment, it would not be feasible.
> > Someone else suggested adding null references to the language: > > ZObj $obj = null; > > But I don't think that is the right solution either not to mention the > nightmare of actually implementing it.
It was supported before(until May), isn't it ? IMHO, what we have now is "not null typehints" so what's the matter if we accept "not null typehints" when we have this construction: function A(ZObj $arg2, ZObj $arg3) {} and "null typehints" with this: function A(ZObj $arg2=null, ZObj $arg3=null) {} as we supported before(except that with the first construction nulls are not allowed). Maybe I'm "blind" but I can't see the problem that Marcus pointed anymore. If I can remember, he wants a way to enforce typehints not allowing nulls: construction-1 does it. I really need optional typehints and construction-2 supports it. IMHO, everybody would be happy. Any comments ? Regards, Cristiano Duarte
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Cristiano Duarte

21 years ago
Robert Silva wrote:
> Re-reading the previous discussion, it seems like the functionality that > people wanted was to have an optional typed argument. > > In converting the .NET Framework library over to PHP, there are many > instances where having optional typed params would have been handy. The > Framework makes heavy use of interfaces, usually implemented as overloaded > methods. When implementing it in PHP I had to make use of func_num_args() > and func_get_args() etc.. to simulate the overloading. As I convert the > classes over to an extension, I am able to define the method arguments as > optional and be of a certain type if they are passed. It would be nice to > have similar functionality in userland. > > The quick solution is to have =null override the zend_verify_arg_type > is_null check, but then this lets users pass null as a value and you lose > your type hint.
PHP doesn't have (until now) null references, so the only "thing" that we can handle as a null reference is the type "null"(which isn't a null reference btw). But I can live with this. :-) What I propose is something like: if the typehinted parameter is optional and the user doesn't pass it, the only "type" it can have (at least now) is "null". So, if you, as a developer, want a function to have optional typehinted parameters, you must check for null to test if the user passed the parameter or not, IMHO it's logical. function f(Obj $a, Obj $b=null, Obj $c=null) {...} You can't call f(null) since the first argument is mandatory, typehinted and doesn't allow nulls, but you can call f(new Obj(), null, new Obj()) since the second argument is optional and allow nulls(the notation =null). The only problem with this approach is the fact that we "maybe" want an optional parameter in which the user can't pass nulls. This is a strange situation since PHP doesn't allow a notation like: f(new Obj(),,new Obj()) And we don't have any language construction to pass instead. I really can't see the point in optional typehinted parameters in which the user can't pass nulls.
> > The solution I proposed makes use of the current method of making > parameters optional, BUT it would still be a runtime error to pass NULL to > the function.
I really don't need to pass NULL to the function but I need optional typehinted arguments(not only one, many).
> > 1. It doesn't add new keywords to the language
+1
> 2. There is no "magic", it would be a runtime error to pass null.
+0
> 3. It does change the "expected" functionality of $obj=default, but it is > limited to the scope of type hints. > 4. Doesn't affect BC (since they can't use a default now)
+1
> 5. It allows optional typed parameters
+1
> 6. You still have to check for null
+1
> 7. Downside - you cant pass optional params after the first one.
-1
> (...)Typed optional params that cannot be null.
-1 Regards, Cristiano Duarte

Robert Silva

21 years ago
Good points... Heres another idea which I also implemented and I think others may like it as well: Explicitly deny anything except for: Method(ClassType $a) And Method(ClassType $a=false); Then inside the function itself: if ($a) $a->DoIt(); Inside the engine, it explicitly checks for the false bool value (this prevents passing null and unitialized vars as well) then overrides the null check. Class designers then can be sure that either an object of the specified class was passed in, or the user wanted to pass no variables at all. Any other value/var would be an error. In essence you are changing the way optional params work when paired with a class hint to say: When you define a Class Hint on an optional parameter, the default value represents the only allowed value to be passed in (other than an object of the proper class). With that then you can do: Method(ClassType $a, ClassType $b) {} $c->Method(false, $b); The implementation was pretty simple and was much more elegant than the first method I suggested. This one I think is workable for the masses. Forget the other ideas (in retrospect they werent very good) but I think this one has promise. Bob -----Original Message----- From: Cristiano Duarte [mailto:cunha17@uol.com.br] Sent: Sunday, October 17, 2004 1:19 PM To: internals@lists.php.net Subject: RE: [PHP-DEV] Type hints with null default values Robert Silva wrote:
> Re-reading the previous discussion, it seems like the functionality that > people wanted was to have an optional typed argument. > > In converting the .NET Framework library over to PHP, there are many > instances where having optional typed params would have been handy. The > Framework makes heavy use of interfaces, usually implemented as overloaded > methods. When implementing it in PHP I had to make use of func_num_args() > and func_get_args() etc.. to simulate the overloading. As I convert the > classes over to an extension, I am able to define the method arguments as > optional and be of a certain type if they are passed. It would be nice to > have similar functionality in userland. > > The quick solution is to have =null override the zend_verify_arg_type > is_null check, but then this lets users pass null as a value and you lose > your type hint.
PHP doesn't have (until now) null references, so the only "thing" that we can handle as a null reference is the type "null"(which isn't a null reference btw). But I can live with this. :-) What I propose is something like: if the typehinted parameter is optional and the user doesn't pass it, the only "type" it can have (at least now) is "null". So, if you, as a developer, want a function to have optional typehinted parameters, you must check for null to test if the user passed the parameter or not, IMHO it's logical. function f(Obj $a, Obj $b=null, Obj $c=null) {...} You can't call f(null) since the first argument is mandatory, typehinted and doesn't allow nulls, but you can call f(new Obj(), null, new Obj()) since the second argument is optional and allow nulls(the notation =null). The only problem with this approach is the fact that we "maybe" want an optional parameter in which the user can't pass nulls. This is a strange situation since PHP doesn't allow a notation like: f(new Obj(),,new Obj()) And we don't have any language construction to pass instead. I really can't see the point in optional typehinted parameters in which the user can't pass nulls.
> > The solution I proposed makes use of the current method of making > parameters optional, BUT it would still be a runtime error to pass NULL to > the function.
I really don't need to pass NULL to the function but I need optional typehinted arguments(not only one, many).
> > 1. It doesn't add new keywords to the language
+1
> 2. There is no "magic", it would be a runtime error to pass null.
+0
> 3. It does change the "expected" functionality of $obj=default, but it is > limited to the scope of type hints. > 4. Doesn't affect BC (since they can't use a default now)
+1
> 5. It allows optional typed parameters
+1
> 6. You still have to check for null
+1
> 7. Downside - you cant pass optional params after the first one.
-1
> (...)Typed optional params that cannot be null.
-1 Regards, Cristiano Duarte
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Jason Garber

21 years ago
Hello, Consider this: public function foo(MyClass $o) If you allow null, false, or anything else, you must CHECK it before using it or you will get a "calling a method on a non-object" first. The whole point of having a type hint, from my point of view, is so the majority of time I DO NOT have to check the incoming parameter before using it. If nulls or false's are allowed, without explicitly saying they are, such as function foo(nullable MyClass $o), then I'll have to either do an is_null() or instanceof check on each parameter. I'm all for it if we have an option added to the function syntax such as nullable, but otherwise, it's usefulness is minimized.
-- Best regards, Jason mailto:jason@ionzoft.com

Ron Korving

21 years ago
> Robert Silva wrote: > What I propose is something like: > > if the typehinted parameter is optional and the user doesn't pass it, the > only "type" it can have (at least now) is "null". So, if you, as a > developer, want a function to have optional typehinted parameters, you
must
> check for null to test if the user passed the parameter or not, IMHO it's > logical. > > function f(Obj $a, Obj $b=null, Obj $c=null) {...} > You can't call f(null) since the first argument is mandatory, typehinted
and
> doesn't allow nulls, but you can call f(new Obj(), null, new Obj()) since > the second argument is optional and allow nulls(the notation =null).
I totally agree with you. IMHO this is exactly how it should work. I've actually been waiting for this feature for a while now, so I'm glad the discussion is active again. Ron

Andi Gutmans

21 years ago
Hi, First of all, I think most agree that it'd be nice to be able to distinct between class instances only, and class instances w/ NULL. However, the main problem was how to specify this in a way which would be very easy to understand and without over-complicating PHP (I wouldn't want to add more complexity to the OO language unless really necessary or we'll loose the ease of use advantage PHP has). 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 problem with it is that it also means that $obj maybe an optional argument so we would have some ambiguity here. I don't like the idea of using other values as the default value like "false" i.e.: function method(MyClass $obj = false); because I think it's very un intuitive of what this means. Actually, we should update the compiler to make sure to not allow optional parameters if class type hints are being used (except for maybe NULL if we do give it some kind of meaning). All in all I haven't been able to think of a good solution/syntax, old ideas like "MyClass $obj or NULL" are more or less OK but then you can use a default parameter so it might become "MyClass $obj or NULL = NULL". Yikes! Andi

Robert Silva

21 years ago
Based on the issues you listed, how about: function Method((MyClass) $obj); function Method([MyClass] $obj); Surrounding the class type in () or [] thereby marking it as optional. Much like you would do when commenting a function to take an optional parameter. Bob Silva -----Original Message----- From: Andi Gutmans [mailto:andi@zend.com] Sent: Monday, October 18, 2004 6:18 PM To: Robert Silva; 'Cristiano Duarte'; internals@lists.php.net Subject: RE: [PHP-DEV] Type hints with null default values Hi, First of all, I think most agree that it'd be nice to be able to distinct between class instances only, and class instances w/ NULL. However, the main problem was how to specify this in a way which would be very easy to understand and without over-complicating PHP (I wouldn't want to add more complexity to the OO language unless really necessary or we'll loose the ease of use advantage PHP has). 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 problem with it is that it also means that $obj maybe an optional argument so we would have some ambiguity here. I don't like the idea of using other values as the default value like "false" i.e.: function method(MyClass $obj = false); because I think it's very un intuitive of what this means. Actually, we should update the compiler to make sure to not allow optional parameters if class type hints are being used (except for maybe NULL if we do give it some kind of meaning). All in all I haven't been able to think of a good solution/syntax, old ideas like "MyClass $obj or NULL" are more or less OK but then you can use a default parameter so it might become "MyClass $obj or NULL = NULL". Yikes! Andi
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Robert Cummings

21 years ago
On Mon, 2004-10-18 at 22:09, Robert Silva wrote:
> Based on the issues you listed, how about: > > function Method((MyClass) $obj); > function Method([MyClass] $obj); > > Surrounding the class type in () or [] thereby marking it as optional. > Much like you would do when commenting a function to take an optional > parameter.
I like this idea and just for the sake of consideration and to elaborate on it: function Method( [MyClass,YourClass,null,bool] $obj ); which would allow all of the above types as values for $obj. Which would allow a great deal of flexibility while retaining a good deal of readability. Cheers, Rob.
-- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

Andi Gutmans

21 years ago
I think it's an interesting idea (the []). I have to think about that one, if it's too confusing and if it's better than something like nullable. BTW, I think it has to be class or NULL. I don't believe any other type should be allowed. Andi At 07:09 PM 10/18/2004 -0700, Robert Silva wrote:

Christian Schneider

21 years ago
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

Jochem Maas

21 years ago
As a point of interest, the error message you get if you pass something to a function which has classhint on the relevant arg which is not an object is as follows: "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:

Todd Ruth

21 years ago
On Tue, 2004-10-19 at 02:42, Christian Schneider wrote:
> 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.
Is it an implementation difficulty that makes that example "not possible"? If not, I don't see a problem with it. It just means the first parameter is optional in the sense that null is allowed. I would be allowed to call "method(null, $param)". "method($param)" would not be legal. In the implementation, all "optional" parameters before the last mandatory parameter would be required to be specified by the caller but allowed to be null. I don't believe the argument that a parameter should be allowed to be optional but not allowed to be specified as null. If I'm allowed to leave out the argument entirely, it doesn't do any harm to give me 2 ways to say I'm leaving out the argument: one by actually not providing an argument and the other by providing a null. There is a subtle difference, but it is a difference that is already difficult make use of in php without func_get_args (not that that's a bad thing). $.02 (well, less than that) - Todd

Marcus Börger

21 years ago
Hello Christian, we've discussed this restriction before and the more i think over the issue the more i come to the conclusion that if at all we should provide this exact syntax. regards marcus Tuesday, October 19, 2004, 11:42:22 AM, you wrote: