Re: [PHP-DEV] Typed properties problems
Hi! > Exactly. Scalars can change datatypes without warning. That's powerful, > but also migrane inducing when not used properly. Not really, scalars can't change datatypes. Variables can hold values of different datatypes, and certain operations can convert values between types. This is the situation with many languages, but in PHP the rules of which operations would accept which types and convert them are more relaxed than in some other languages. -- Stas Malyshev smalys...@gmail.com -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Typed properties problems
On Tue, Jan 17, 2017 at 4:52 PM, Stanislav Malyshev wrote: > Hi! > > > That's pretty messed-up right there. At least in the object cases there's > > an E_WARNING "Creating default object from empty value" - with the array > > case, nothing, it just converts your boolean into an array. Yikes. > > You told PHP you want an array, you get an array. > > Exactly. Scalars can change datatypes without warning. That's powerful, but also migrane inducing when not used properly. I taught myself PHP. For the first two years I had no real grasp of the significance of datatypes, and it would be another two before I felt a need to want to control datatypes. Strict datatyping makes a language harder to use for beginners. Loose datatyping can make debugging harder on veterans. For that reason PHP should ideally be able to do both. A step in that direction was taken in 7 with the strict type declarations for function arguments and returns. Applying this to properties would be nice, but the coercion rules for situations like Stanislav gave are going to be difficult.
Re: [PHP-DEV] Typed properties problems
Hi! >> That's pretty messed-up right there. At least in the object cases there's >> an E_WARNING "Creating default object from empty value" - with the array >> case, nothing, it just converts your boolean into an array. Yikes. This works only on "empty" values, btw - null, false and "". And it doesn't really "convert" anything - no more than reassigning variable converts anything - it just drops the "empty" value and replaces it with an array. -- Stas Malyshev smalys...@gmail.com -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Typed properties problems
Hi! > That's pretty messed-up right there. At least in the object cases there's > an E_WARNING "Creating default object from empty value" - with the array > case, nothing, it just converts your boolean into an array. Yikes. You told PHP you want an array, you get an array. -- Stas Malyshev smalys...@gmail.com -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Typed properties problems
On regular PHP 7, the array weirdness (Dmitry's last example) doesn't even produce a warning: class Shit { public $yo = false; } $shit = new Shit(); $shit->yo[] = "what"; // quitly turns $yo into an array! echo gettype($shit->yo); // array That's pretty messed-up right there. At least in the object cases there's an E_WARNING "Creating default object from empty value" - with the array case, nothing, it just converts your boolean into an array. Yikes. On Tue, Jan 17, 2017 at 11:03 AM, Dmitry Stogov wrote: > Hi Bob, > > > I've found a number of problems: > > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b->ops += 5; echo gettype($x->b),"\n";' > > object > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b->ops++; echo gettype($x->b),"\n";' > object > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b->ops = 5; echo gettype($x->b),"\n";' > object > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b[] = 5; echo gettype($x->b),"\n";' > array > > Thanks. Dmitry. > >
Re: [PHP-DEV] Typed properties problems
https://github.com/php/php-src/compare/master...bwoebi:typed_ref_properties From: guilhermebla...@gmail.com Sent: Tuesday, January 17, 2017 6:02:09 PM To: Dmitry Stogov Cc: Bob Weinand; Joe Watkins; PHP internals list Subject: Re: [PHP-DEV] Typed properties problems Where can I see progress of this work? On Tue, Jan 17, 2017 at 5:03 AM, Dmitry Stogov mailto:dmi...@zend.com>> wrote: Hi Bob, I've found a number of problems: $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b->ops += 5; echo gettype($x->b),"\n";' object $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b->ops++; echo gettype($x->b),"\n";' object $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b->ops = 5; echo gettype($x->b),"\n";' object $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b[] = 5; echo gettype($x->b),"\n";' array Thanks. Dmitry. -- Guilherme Blanco Senior Technical Architect at Huge Inc.
Re: [PHP-DEV] Typed properties problems
Where can I see progress of this work? On Tue, Jan 17, 2017 at 5:03 AM, Dmitry Stogov wrote: > Hi Bob, > > > I've found a number of problems: > > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b->ops += 5; echo gettype($x->b),"\n";' > > object > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b->ops++; echo gettype($x->b),"\n";' > object > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b->ops = 5; echo gettype($x->b),"\n";' > object > > $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; > $x->b[] = 5; echo gettype($x->b),"\n";' > array > > Thanks. Dmitry. > > -- Guilherme Blanco Senior Technical Architect at Huge Inc.
[PHP-DEV] Typed properties problems
Hi Bob, I've found a number of problems: $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b->ops += 5; echo gettype($x->b),"\n";' object $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b->ops++; echo gettype($x->b),"\n";' object $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b->ops = 5; echo gettype($x->b),"\n";' object $ sapi/cli/php -r 'class Foo {public bool $b = false;} $x = new Foo; $x->b[] = 5; echo gettype($x->b),"\n";' array Thanks. Dmitry.