0

Could you please explain what does the operator ?-> do in PHP, since I have this piece of code:

$drive = $objDrive?->func?->getDriver()?->value;
Oscar Gallardo
  • 1,262
  • 3
  • 16
  • 38
  • 3
    The answer to this question should be placed in [What does this symbol mean?](/questions/3737139/reference-what-does-this-symbol-mean-in-php). – John Conde Jun 03 '20 at 16:57
  • as far as I know, it is a new operator in the programming language, thanks for your suggestion – Oscar Gallardo Jun 03 '20 at 17:01
  • 1
    The code you have written there will not work in any released version of PHP, and may or may not ever do so. The feature was only officially proposed yesterday, and hasn't even been discussed yet. – IMSoP Jun 03 '20 at 17:48

1 Answers1

2

For the moment, it's just a proposal, you can find it enter link description here. It's the NullSafe Operator, it returns null in case you try to invoke functions or get values from null... Example

$objDrive = null;
$drive = $objDrive?->func?->getDriver()?->value; //return null
$drive = $objDrive->func->getDriver()->value; // Error: Trying to get property 'func' of non-object
Berto99
  • 7,767
  • 2
  • 7
  • 22
  • 2
    Can you move this answer to [here](https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php)? That is the canonical place for these type of questions. – John Conde Jun 03 '20 at 17:08
  • I believe it's better to have it (also) as a separate answer since it will be much easier to find. – Andreas Jun 03 '20 at 17:15
  • @JohnConde idk how to do it aha feel free to do it – Berto99 Jun 03 '20 at 17:17
  • @Berto99 Just copy and paste it :) I'll do that now – John Conde Jun 03 '20 at 17:18
  • @Andreas yes, so do I, so than if google index it, or someone search for that operator, it can be found here, and so it has not to read the whole page – Berto99 Jun 03 '20 at 17:18
  • 2
    @Andreas That other question is what we use for all questions about PHP symbols since they are hard to search for. Stack Overflow does not like answers being duplicated and wants one canonical source for all knowledge wherever possible. – John Conde Jun 03 '20 at 17:19
  • @JohnConde for what i can see there i can find the operator approved, not the proposed, so if you think that that page is the place where this answer should be, feel free to copy it and paste it on that page – Berto99 Jun 03 '20 at 17:21
  • @JohnConde *...PHP symbols since they are hard to search for.* But also impossible to find on that page. *- what does this mean?* And someone replies with duplicate of 60 pages. Not easy for someone to find something there. Sure easy for closing but not easy for the one asking. – Andreas Jun 03 '20 at 17:23
  • 1
    @Andreas This is what the community has decided should be done and it has worked very well. If you want to change this you can bring it to meta but I don't think you will receive a warm response. – John Conde Jun 03 '20 at 17:24
  • @JohnConde posted on that page – Berto99 Jun 03 '20 at 17:24
  • 1
    @JohnConde I personally super-hate the canonicals that are a mile long. I have found a more specific duplicate and added to the list. – mickmackusa Nov 27 '20 at 22:31