"Error" referring to the question mark (?) used in PHP function statements

Asked

Viewed 39 times

0

Not long ago, I was taking a course from a youtube playlist..

And, everything was going ok.. until.. I came across the question mark, where the author used in the functions.

In my case it was a mistake and in class everything was ok, so much so that the author did not even mention this point.

If I removed the question mark, it would not be wrong. But I was in doubt.. if doing so the code would have some behavior different from the author, or not.

So I got discouraged.. and went to do something else! But.. wanted to return to the course.. because the content was interesting.. so the request for help here.

An example of where the author applied the question:

public static function getString(): ?string { .. }

Just the fact that he put the " : ?string " I found it strange, because until then I had not come across codes made this way!

I’ve seen it in other languages, but I haven’t seen it in PHP. So I think he is "forcing" the function to return a String value, otherwise it will be an error. So far so good, but.. what this query means before the "string " ??!!!!

Another example of him:

public function find(?string $terms = null): ?DataLayer
{ .. }

In the latter still uses inside brackets of parameters.

I don’t have now the mistake that generated me, because I "abandoned" the course!!

But a little help and explanation would help.

I read something about, here in the same forum, something similar but it was using Laravel, and one of the answers said the following:

The version does not match the new feature called Nullable types which was introduced in version 7.1, so 7.0 does not have this feature therefore of the mistakes.

If it’s a PHP incompatibility, I can’t say.. but I’m using PHP 7.4.4.

Anyway.. I’m waiting for some help.

Thank you.

  • The question mark ?string says the function can return a string or a value null. When it is used in assignment ?string $var = null says the variable can receive a null value or a string.

  • Here is an answer cool made by @Mineiro, recommend reading to be careful with this type of attribution.

  • Referring to "ternary operator".. I know how it works. E.. this is not ternary operator!! Only if it is something "new" and I don’t know. @Boi Programmer, this answer is exactly what I had in mind!!! But still remains my question!! Which is why PHP is not recognizing??? Obs.:.. marked as "duplicate", but I didn’t find anything about it.. the way I was wrong!!!.. Most I found referred to..

  • To give you an answer you would need the exact error that occurred, but I can only guess one possible cause of the error: The version of your PHP, this kind of return and attribution statement is only possible from version 7 of PHP. As for this return ?DataLayer seems to be the return of an instance of the class itself, would need more information in your question to be able to elaborate an answer.

  • @ Programmed Bull.. Got it. The "error" was not exactly referring to "Datalayer", up to pq.. this is part of the code created by the author. What I was doing.. to get on with his course, was.. remove that question. But I don’t think it’s the best way. I will resume the course and post the error message which php returns. But php is not recognizing anything of the use of the question!! Except for the ternary, which is not the case. .

  • If you are withdrawing the ? and is working is pq the function has not returned value null. Some time she will return null and your code will generate an error.

  • I created an example in Pastebin, take a look there.

  • Okay.. I’ll see.. and test.. Thank you..

Show 3 more comments
No answers

Browser other questions tagged

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