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

Reply via email to