2

Feel free to re-title this Question because I do not know the proper name for doing this. More to the point, I have seen people using {'property'} when accessing a property inside an object so I set-up an example to try understand however, the property is accessible when I use it and when I don't?

class Example {
    public $name;
}

$e = new Example();
$e->{'name'} = 'Kdot';

echo $e->name; // output: Kdot

I have tried changing the scopes and accessing it through a class method but it works both ways, again.

Can someone help me understand what the meaning of using the {} delimiters are? Because from my knowledge, if you stored the parameter inside another variable, this would also work:

$property = 'name';
echo $e->$property; // output: Kdot
Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Many ways to do the same thing. Like in JS, you can do `window['onload']`, `window.onload` and `var name = onload; window[name]` – Ruan Mendes Nov 17 '16 at 22:34
  • Is it just best-practice? @JuanMendes – Jaquarh Nov 17 '16 at 22:34
  • See http://stackoverflow.com/questions/11940244/how-to-access-an-object-property-with-a-minus-sign – Sam Dufel Nov 17 '16 at 22:35
  • It's not best practice to use the `{'property'}` notation, but it might be used in a specific project if they have a lot of properties that aren't valid identifiers (e.g. `{'%%property%%'}` would not be a valid identifier, but could be used as a property if it's accessed this way). – Jason Nov 17 '16 at 22:37

1 Answers1

2

They're mostly both just different ways to do the same thing, but with slightly different capabilities.

One difference is that if you want to concatenate inline and use that for a property name, you need the braces:

// example:
$property = 'foo';

echo $e->{$property . 'Suffix'}; // good, uses $e->fooSuffix
echo $e->$property . 'Suffix'    // bad, uses $e->foo and adds "Suffix" literal

As well as that, directly access property names need to conform to certain rules. For example if you have a dash in your property name you need to use braces to access it.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • Nice, I was struggling trying to think of an example where you needed `obj->${}` – Ruan Mendes Nov 17 '16 at 22:36
  • Thanks, It was probably because I have been searching using the words prefix and delimiters that I haven't found this information. I appreciate this and will probably start using braces from now on. – Jaquarh Nov 17 '16 at 22:37
  • Actually, I imagine it might be more applicable to dynamic method names rather than properties... just an example though :) – scrowler Nov 17 '16 at 22:37
  • @KDOT I'd actually suggest that you only use braces when you need to, otherwise it just adds unnecessary clutter to the code. – scrowler Nov 17 '16 at 22:37
  • It look's so clean though hahahha #OCD @RobbieAverill – Jaquarh Nov 17 '16 at 22:39
  • In that example it does, yeah - but for example `$e->{'my_var'}` seems excessive when compared to `$e->my_var` – scrowler Nov 17 '16 at 22:39
  • 1
    You killed my vibe on using braces with that example ;) Thank's for the explanation and the resources though! @RobbieAverill – Jaquarh Nov 17 '16 at 22:41