PHP strict standards: is this bad?
Asked Answered
A

6

5

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

Active answered 21/7, 2011 at 19:1 Comment(4)
i didn't even know i could do that with = null; lolDielu
I wonder how do you explain the code in your first example? How can a null have any properties?Harville
The fact that he can set $text->name when $text doesn't exist at all... is telling.Lenny
@PeeHaa: You should have left it! Like I said... it was telling.Lenny
L
11

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.

Lenny answered 21/7, 2011 at 19:4 Comment(2)
-1 for emphasis on the right without thinking about it. Plus, it doesn't even answer the question. I get that $test = null for object making is real dirty, but the second "Yes" doesn't mean a damn thing, it doesn't even make sense. Now for the right thing. If it's said to be right (by who?), doesn't mean it is right in every scenario, or that whoever said it first was right. What do we really gain/loose by sticking/not sticking to strict standards? And, Please, don't answer "Yes" to this question.Hamper
@enrey: It makes sure code keeps on working in future versions, and that it will be better backwards compatible. It is also a matter of best practice. Can't you read? Don't accuse of me of "not thinking about it" when you're clearly at fault in that regard.Lenny
B
4

null is not an object. You'd essentially doing:

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

... and you wonder why you get warnings?

Blackthorn answered 21/7, 2011 at 19:5 Comment(0)
W
3

Using this :

$test = null;

You are not making clear (to both the PHP engine, and people who will read your code) that $test is an object.


So, it doesn't feel natural, as a reader, to later find out that $test is used as an object -- which means your code is less maintenable than when you are explicitely declaring $test as an object.

Whiles answered 21/7, 2011 at 19:5 Comment(0)
D
1

It's awfully bad to do and to read that = null; go with the standard new ...;

Dielu answered 21/7, 2011 at 19:5 Comment(0)
H
1

If you make a habit of actually declaring your objects correctly with $test = new stdClass(); then seeing this strict-mode error will help you catch implicit variable declaration errors that PHP loves to throw at you when you accidentally type $usr for $user and the like, saving you pointless headaches.

Homemade answered 21/7, 2011 at 19:7 Comment(0)
P
1

You could cast something to an object like this:

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

But new StdClass() is still the most strict way in doing it. Although I have this feeling you only want the error gone and might like this syntax better.

Pastiche answered 21/7, 2011 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.