Strict PHP standards: is it bad?

When I create a standard class, I basically do:

$test = null;
$test->id = 1;
$test->name = 'name';

However, in strict mode, I get an error message.

Therefore, obviously, the correct way to do this is:

$test = new stdClass();
$test->id = 1;
$test->name = 'name';

Therefore, I wonder:

This is a big no-no-do: $test = null;do what I want?

What do we get by following strict standards? Does it ensure that the code will work in future versions? Would it be better backwards compatible? Is this just a matter of best practice? Something else?

EDIT typo

+5
source share
6 answers

This is a big no-no: $ test = null; do what i want?

Yes.

This is allowed because PHP is free, but enabling strict mode gives you honest truth.

, ? ? ? ?

.

- ?

.

+11

null . :

$test = 'this is not an object';
$test->suddenly_it_is_an_object = true;

... , ?

+4

:

$test = null;

( PHP, , ), $test .


, , , , $test , , , $test .

+3

, = null; new ...;

+1

$test = new stdClass();, , PHP , $usr $user .., .

+1

- :

$object = (object) array(
    'id' => 5,
    'name' => 'Darsstar',
    'key' => 'value',
);

StdClass() - . , , .

+1

All Articles