Jump to content


Photo

{ And }


  • Please log in to reply
5 replies to this topic

#1 sneskid

sneskid
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 29 June 2006 - 02:03 AM

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)) {
$d->{urldecode($key)} = $this->urldecodeRecursively($val);
}
else {
$d->{$key} = $this->urldecodeRecursively($val);
}
}
return $d;
//

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

thanks.

#2 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 29 June 2006 - 02:12 AM

stclass() is an object
$d->{whatever} calls the object, as in, makes it work, like in this example.

[code=php:0]
<?PHP
class test {
function test() {
echo "test";
}
}

$test = new test;
$test->test();

That would echo test.

#3 sneskid

sneskid
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 29 June 2006 - 03:07 AM

I'm clear on that. but why did he use { }

what's the difference between
myClass->myFunction();
myClass->{myFunction()};

when is it appropriate to use {} in this context

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 29 June 2006 - 03:17 AM


//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}

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 June 2006 - 10:16 AM

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 value of the $key variable. So if $key was set to SomeVar, $var will be replaced with its value when it is parsed like so:
$d->SomeVar = $this->urldecodeRecursively($val);

Hope that helps. Basically { and } are used to define functions/variables

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 June 2006 - 12:22 PM

wildteen's got it exactly right... i like to think of it like this: the brackets allow you to force 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:
$string = "obsidian";
${$string} = "me";
echo $obsidian; // outputs "me"

it's not exactly the most simple concept to grasp, although it is a very helpful one to get ahold of.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users