What's the difference between these 2 piece of codes?
<?php
$object1 = new User();
//^^
$object1->name = "Hello";
echo $object1->name;
class User {}
?>
And:
<?php
$object1 = new User;
//^
$object1->name = "Hello";
echo $object1->name;
class User {}
?>
I get the same output:
Hello
So is there any difference if I use the parentheses or not in:
$object1=new User;
The are exactly the same, you can compare opcode of these 2 scripts:
1 script:
opcode:
2 script:
opcode:
Both are equal. if you not using any code convention then use which you like. I think
$object1 = new User()
would be useful over$object1 = new User
. if you were passing arguments to the constructor.