What is the correct option to instantiate a PHP class?

Asked

Viewed 463 times

6

What is the correct option to instantiate a PHP class?

Whereas the class is under discussion if call Atleta:

1)

$atleta=Atleta;

2)

$atleta= new Atleta();

3)

$atleta= Atleta();

Which of the 3 options is the right way to instantiate a PHP class?

  • IS $atleta= new Atleta();

  • Ah I prefer $atleta = new Atleta();.

  • 2

    @Tmc this edition made the title more confusing, it was already quite clear. So I reverted its edition to the previous one.

  • @Articuno, I agree I went to review and made it more confusing

  • 1

    @Stormwind would only take the highlight of quote, which for this case would not even be necessary, but had not noticed that there were 3 forms, the edition was good.

3 answers

8

In the context of the question would be the second option:

$atleta = new Atleta();

But remember that in PHP, it is not necessary to use parentesis () when the class has no constructor or does not need arguments.

Then that would also be valid:

$atleta = new Atleta;

Explanation of each option

I don’t know if I’m wrong, but the question sounds a lot like an evaluative question. So, assuming that, I don’t think it’s cool to just "give the right answer," but explain what each thing does.

$atleta = Atleta;

Generally, this syntax is used in PHP to get the value of a constant.

For example:

const Atleta = 'Atleta';

// ou 

define('Atleta', 'Atleta');

$atleta = Atleta;

Observing: When trying to assign the value of an undefined constant, you will receive an error message of type E_NOTICE and the value assigned will be a string as the name of the nonexistent constant.

$atleta= Atleta();

This syntax is used for the direct function call. Functions in PHP are called with the use of parentesis, can be passed arguments or not.

Example:

 function Atleta() {
       return 'Atleta';
}

$atleta = Atleta();
  • The last example can return an object: function Atleta() { return new Atleta }, it would not cease to be an instance of an object, so with the exception of the first example of AP, the others would be 'correct'. It’s just a supplement to the answer, I know you’re aware :)

  • I was not explaining what can return, but rather the syntax that refers to each item

  • Who gave the negative, could explain the reason? What is wrong in the answer?

7

Is to use option 2 that way:

$atleta= new Atleta();

More information can be found here php.net

4

According to the official website of PHP, the instruction should be used new to instantiate a class.

If you make instance of an empty generic object the best way is to use the method below:

$obj = new obj(); 

Being the most correct and fast way.

Only going further, in php7 there are other ways to instantiate an empty object:

$obj1 = new \stdClass; //instancia o objeto stdClass
$obj2 = new class{}; //instancia uma classe anônima.

In short, the most used by the community: Libraries, frameworks and etc is with the square brackets.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.