RE: [PHP-DEV] [RFC] Alternative typehinting syntax for accessors

From: Date: Tue, 08 Jan 2013 07:56:46 +0000
Subject: RE: [PHP-DEV] [RFC] Alternative typehinting syntax for accessors
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi. I like the proposal of this RFC very much ;-)

But the way 'nullable' properties are defined is not very intuitive and unclean, in my
opinion. Stas has already mentioned that. 
public DateTime $date = NULL; // this looks like the property is initialized with null,
but it does not show that the property is 'nullable'

If type hinted properties are not allowed to be set to null, I would propose this way to make a
property nullable:

    public DateTime? $date;

In C# the question mark after a type is a short hand for a generic Nullable type.


-----Original Message-----
From: Nikita Popov [mailto:[email protected]] 
Sent: Saturday, January 05, 2013 1:48 AM
To: Steve Clay
Cc: PHP internals
Subject: Re: [PHP-DEV] [RFC] Alternative typehinting syntax for accessors

On Sat, Jan 5, 2013 at 12:31 AM, Steve Clay <[email protected]> wrote:

> Would the following two be exactly functionally equivalent?
>
> public Foo $foo;
>
> public $foo {
>     get;
>     set(Foo $value) { $this->foo = $value; }
> }
>
> We should also consider how an author would allow type-hinted properties
> to accept NULL as a new value, in both proposals.
>

I think that's a very interesting question, thanks for bringing it up. I
think a good approach here would be the same one used for function argument
typehints, i.e. allow NULL when NULL is specified as the default value. So
public DateTime $date; would not allow an explicit NULL assignment,
whereas public DateTime $date = NULL; would.

Nikita


Thread (47 messages)

« previous php.internals (#64655) next »