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.
Where can I see progress of this work?
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";'
arrayThanks. Dmitry.
--
Guilherme Blanco
Senior Technical Architect at Huge Inc.
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. Yiiiiikes.
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";'
arrayThanks. Dmitry.
Hi!
That's pretty messed-up right there. At least in the object cases there's
anE_WARNING
"Creating default object from empty value" - with the array
case, nothing, it just converts your boolean into an array. Yiiiiikes.
You told PHP you want an array, you get an array.
--
Stas Malyshev
smalyshev@gmail.com
Hi!
That's pretty messed-up right there. At least in the object cases there's
anE_WARNING
"Creating default object from empty value" - with the array
case, nothing, it just converts your boolean into an array. Yiiiiikes.
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
smalyshev@gmail.com
On Tue, Jan 17, 2017 at 4:52 PM, Stanislav Malyshev smalyshev@gmail.com
wrote:
Hi!
That's pretty messed-up right there. At least in the object cases there's
anE_WARNING
"Creating default object from empty value" - with the array
case, nothing, it just converts your boolean into an array. Yiiiiikes.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.
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
smalyshev@gmail.com