Re: [VOTE] Alternative typehinting syntax for accessors
| From: | Nikita Popov | Date: | Mon, 21 Jan 2013 22:59:52 +0000 |
| Subject: | Re: [VOTE] Alternative typehinting syntax for accessors | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-65061@lists.php.net to get a copy of this message | ||
On Mon, Jan 21, 2013 at 11:54 PM, Crypto Compress <
cryptocompress@googlemail.com> wrote:
> Hello Nikita,
>
> for me this "maybe-of-type"-typehinting is a small progression in the right
> direction.
>
> But "Default values and nullability" would break existing code as you need
> to add a default null at "design-time" (BC break):
>
> <?php
> class C {
> public $a; // public string $a = null;
> public $b; // public string $b = null;
> public function __construct($a, $b) {
> $this->a = $a;
> $this->b = $b;
> }
> }
>
> // test only a
> $c = new C("foo", null);
> var_dump($c->a === "foo");
>
> // test only b
> $c = new C(null, "foo");
> var_dump($c->b === "foo");
> ?>
>
> http://3v4l.org/4oi9k
>
> cryptocompress
>
Sorry if it wasn't clear: The nullability stuff only applies if a typehint
is used. If no typehint is used, then you can freely assign null,
regardless of whether or not you have the "= null" in the declaration (no
typehint = everything's valid). "public $foo;" will continue to behave
exactly the same way it did before :)
Nikita