Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP strict standards: is this bad?

Tags:

php

stdclass

When I create a standard class I mostly do:

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

However in strict-mode I get an error.

So obviously the correct way of doing it is:

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

So I am wondering:

Is it a big no-no to do: $test = null; to do what I want?

What do we gain by conforming to the strict standards? Does it make sure code will keep on working in future versions? Will it be better backwards compatible? Is it just a matter of best practice? Something else?

EDIT typo

like image 829
PeeHaa Avatar asked Jul 21 '11 19:07

PeeHaa


1 Answers

Is it a big no-no to do: $test = null; to do what I want?

Yes.

It's allowed because PHP is loose, but turning on strict mode gives you the god's-honest truth.

What do we gain by conforming to the strict standards? Does it make sure code will keep on working in future versions? Will it be better backwards compatible? Is it just a matter of best practice?

Yes.

Something else?

It's right.

like image 125
Lightness Races in Orbit Avatar answered Nov 14 '22 22:11

Lightness Races in Orbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!