PHP Class Instantiation. to Use or Not to Use the Parentheses

PHP class instantiation. To use or not to use the parentheses?

They are equivalent. If you are not coding by any code convention, use which you like better. Personally, I like to leave it out, as it is really just clutter to me.

Instantiate a class with or without parentheses?

Any difference in performance is going to be absolutely negligible.

While both ways are fine, I personally would prefer using new Car(); because usually, a method is being called here, and function/method calls in PHP require (). Also, it's more consistent with instantiations that have parameters.

But in the end, it's down to taste. It doesn't matter which way you choose, but when you choose one, stick to it consistently!

Is there a difference between instantiation with parentheses or without?

The are exactly the same, you can compare opcode of these 2 scripts:

1 script:

$object1=new User();
$object1->name="Hello";
echo $object1->name;
class User {}

opcode:

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
3 0 > FETCH_CLASS 4 :0 'User'
1 NEW $1 :0
2 DO_FCALL_BY_NAME 0
3 ASSIGN !0, $1
4 4 ASSIGN_OBJ !0, 'name'
5 OP_DATA 'Hello'
5 6 FETCH_OBJ_R $5 !0, 'name'
7 ECHO $5
6 8 NOP
9 > RETURN 1

2 script:

$object1=new User;
$object1->name="Hello";
echo $object1->name;
class User {}

opcode:

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
3 0 > FETCH_CLASS 4 :0 'User'
1 NEW $1 :0
2 DO_FCALL_BY_NAME 0
3 ASSIGN !0, $1
4 4 ASSIGN_OBJ !0, 'name'
5 OP_DATA 'Hello'
5 6 FETCH_OBJ_R $5 !0, 'name'
7 ECHO $5
6 8 NOP
9 > RETURN 1

When to add brackets after new class name?

The purpose of the brackets is for you to enter any arguments that your constructor may accept.

class Example{

private $str;

public function __construct($str){
$this->str = $str;
}

public function output(){
echo $this->str;
}

}

$ex = new Example; // missing argument error
$ex = new Example('Something');
$ex->output(); // echos "Something"

If your class constructor does not accept any arguments, you may leave the brackets out. For good code sake, I always keep the brackets, whether or not the constructor accepts any argument.

Most coders coming from C# or Java background would keep the parenthesis as it is more familar to them.

Php: Parentheses when creating new object?

Doesn't matter as long as your code is consistent, conventional & clear.

Class instantiation with and without parantheses

In the second case, you're not instantiating the class, just giving it another name, tesla. The parentheses are required for instantiation.

PHP class instantiation. To use or not to use the parentheses?

They are equivalent. If you are not coding by any code convention, use which you like better. Personally, I like to leave it out, as it is really just clutter to me.

What's the difference in new stdClass() and new stdClass?

There is no difference. PHP lets you omit the parentheses if you're not passing arguments to the constructor.



Related Topics



Leave a reply



Submit