Jump to content

Curly Brackets


Drongo_III
 Share

Recommended Posts

Hi Guys

 

Quick noob question.

 

I am still fairly new to oop and i'm trying to also build my first simple mvc to learn about that too.

 

I came across some syntax use in a tutorial that works but i don't really understand why and don't know what this is called to search for it on google.

 

I've instantiated the class and then i pull out and explode the url to make controller or function calls in the mvc. The noob bit i don't understand is this

 

 $controller->{$url[1]}();

 

Why do the curly brackets allow you to place a variable name? And what is this type of syntax called so i can read about it.

 

Any help would be massively appreciated!

 

Drongo

 

 

Link to comment
Share on other sites

To be more verbose...

 

You can call a function in two ways:

1) 

$someClass->myFunction();

 

2) 

$functionName = 'myFunction';

$someClass->{$functionName}();

 

What you are seeing in this code is an array of strings, one of which is being used as a function name.

 

(There are actually many more ways to call functions, I'm just trying not to break your brain).

 

 

Link to comment
Share on other sites

Thanks guys

 

That really helps a lot. I had not come across this way of calling a function in a class before and i can't bring myself to just accept that it works without understanding it as its obviously not the best way to learn. So thank you very much for setting me straight. :)

 

Drongo

 

 

 

 

To be more verbose...

 

You can call a function in two ways:

1) 

$someClass->myFunction();

 

2) 

$functionName = 'myFunction';

$someClass->{$functionName}();

 

What you are seeing in this code is an array of strings, one of which is being used as a function name.

 

(There are actually many more ways to call functions, I'm just trying not to break your brain).

Link to comment
Share on other sites

The complex notation can be used around any variable in about any situation.  It's generally not required however, except in instances where your variable name may be ambiguous or contains characters not normally allowed in a name.

 

In your original case, without the braces:

$controller->$url[1]();

 

That code could mean two things:

1) Find the variable $url and get it's value.  Use that value as the name on $controller, get it's [1]'th element then call it as a function.

-or-

2) Find the variable $url and get it's [1]th element, use that as the name of a method on $controller and call it

 

The author wants the latter,  and the braces tell PHP to do exactly that.

 

Other cases my be something like if your putting a variable in a string next to some text, eg maybe:

$hiliFront = '<strong style="background: yellow;"><em>';
$hiliBack='</em></strong>';

echo "Welcome {$hiliFront}Kicken{$hiliBack}, how are you today";
//Without the braces, PHP would see the first var as $hiliFrontKicken instead of $hiliFront

 

Or if a property on an object contains invalid characters, such as say:

$sql = 'SELECT COUNT(*) FROM bleh';
$obj = mysql_fetch_object(mysql_query($sql));

echo $obj->{'COUNT(*)'}; //Though a better solution is to alias the column name

 

 

Link to comment
Share on other sites

Thanks Kicken

 

That really helps a lot to understand it's use.

 

I suppose part of it is understanding how php will interpret the syntax and what it interprets first - and thinking in that way.  I can see much more clearly that the curly braces allow for some very handy flexibility. I was trying all sorts to make that function call work without using curly braces and nothing did the trick.

 

Think this is one to etch on the noggin! - or in english, 'remember'.

 

Thank you so much for your help. This forum has helped me learn so much it's amazing :)

 

 

 

 

The complex notation can be used around any variable in about any situation.  It's generally not required however, except in instances where your variable name may be ambiguous or contains characters not normally allowed in a name.

 

In your original case, without the braces:

$controller->$url[1]();

 

That code could mean two things:

1) Find the variable $url and get it's value.  Use that value as the name on $controller, get it's [1]'th element then call it as a function.

-or-

2) Find the variable $url and get it's [1]th element, use that as the name of a method on $controller and call it

 

The author wants the latter,  and the braces tell PHP to do exactly that.

 

Other cases my be something like if your putting a variable in a string next to some text, eg maybe:

$hiliFront = '<strong style="background: yellow;"><em>';
$hiliBack='</em></strong>';

echo "Welcome {$hiliFront}Kicken{$hiliBack}, how are you today";
//Without the braces, PHP would see the first var as $hiliFrontKicken instead of $hiliFront

 

Or if a property on an object contains invalid characters, such as say:

$sql = 'SELECT COUNT(*) FROM bleh';
$obj = mysql_fetch_object(mysql_query($sql));

echo $obj->{'COUNT(*)'}; //Though a better solution is to alias the column name

Link to comment
Share on other sites

Other cases my be something like if your putting a variable in a string next to some text, eg maybe:

$hiliFront = '<strong style="background: yellow;"><em>';
$hiliBack='</em></strong>';

echo "Welcome {$hiliFront}Kicken{$hiliBack}, how are you today";
//Without the braces, PHP would see the first var as $hiliFrontKicken instead of $hiliFront

 

Or:

 

$hiliFront = '<strong style="background: yellow;"><em>';
$hiliBack='</em></strong>';

echo "Welcome ".$hiliFront."Kicken".$hiliBack.", how are you today";

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.