Jump to content

{ And }


sneskid

Recommended Posts

I read that "Sometimes PHP programmers need to use "{" and "}" to resolve ambiguity"
The example wasn't enough.

I'm currently going through someone elses code, and they use {} in the following syntax

// this code is part of a class function called "urldecodeRecursively"
$d = new stdClass();
foreach ($data as $key => $val)
{
if ($key != urldecode($key)) {
[b]$d->{urldecode($key)}[/b] = $this->urldecodeRecursively($val);
}
else {
[b]$d->{$key}[/b] = $this->urldecodeRecursively($val);
}
}
return $d;
//

I'm just curious about the $d->{stuff}
Could someone shed some light on that?

thanks.
Link to comment
Share on other sites


//This one wont call a class object {becouse it can not execute}.
myClass->myFunction();


//This one will it is in correct coding pratice for a class object.
myClass->{myFunction()};

{use this to execute a code in an object as exsplined above}
Link to comment
Share on other sites

The { and } are used to basically help PHP out, otherwise it'll get confused. So if the code was this:
$d->urldecode($key) = $this->urldecodeRecursively($val);

PHP will try to called a function called urldecode inside the stdClass. Which probably doesnt exist and end up causing an error! But we want to use a pre-defined PHP function called urldecode instead. So we use the curly brakets to define the function we want to use.

The same applies to this code:
$d->{$key} = $this->urldecodeRecursively($val);
But instead of calling a function we want to use the [b]value[/b] of the $key variable. So if $key was set to [b]SomeVar[/b], $var will be replaced with its value when it is parsed like so:
$d->[i]SomeVar[/i] = $this->urldecodeRecursively($val);

Hope that helps. Basically { and } are used to define functions/variables
Link to comment
Share on other sites

wildteen's got it exactly right... i like to think of it like this: the brackets allow you to [b]force[/b] PHP to translate the values inside them first. that's why sometimes, when we want to create a variable named from the value of another variable, we use the brackets, too:
[code]
$string = "obsidian";
${$string} = "me";
echo $obsidian; // outputs "me"
[/code]

it's not exactly the most simple concept to grasp, although it is a very helpful one to get ahold of.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.