Use curly brackets to structure code in PHP

2019-01-09 09:08发布

Is it possible to enclose code fragments in PHP within brackets (without using the fragment as a function)?

Would the following code behave the same way as it would without the curly brackets? Or might there be any problems depending on what kind of code used inside or outside the brackets?

For example, will this:

<?php

// First Code-Block
{# several lines of code
}

// Second Code-Block
{# another several lines of code
}

?>

Always behave the same way as this:

<?php

// First Code-Block
# several lines of code

// Second Code-Block
# another several lines of code

?>

4条回答
疯言疯语
2楼-- · 2019-01-09 09:47

In one project I'm working on, I use statement-groups to indicate structure - in my case, parent/child relationships between nodes creates in a router:

$router = new Router();

$admin = $router->route('admin');
{
    $upload = $admin->route('upload')->post('upload');

    $menu = $admin->route('menu');
    {
        $menu->route('load')->get('load');
        $menu->route('save')->get('save');
    }
}

```

Internally, this builds a hierarchical structure like:

/admin
  /upload
  /menu
    /load
    /save

Calling route() in this example creates a child - so the code creates a model (inside the router) which has a tree structure, but the structure of the code does not reflect that.

I'm using curly braces here to make the code more legible, since reading the code without curly braces and indentation would be quite difficult:

$router = new Router();

$admin = $router->route('admin');
$upload = $admin->route('upload')->post('upload');
$menu = $admin->route('menu');
$menu->route('load')->get('load');
$menu->route('save')->get('save');

Indentation in this case really clarifies what's happening, I think.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-09 09:48

PHP code behavior does not change if you enclose it within curly brackets. However, you can't use some PHP statements inside curly brackets:

  • namespace declarations;
  • namespace use declarations to alias or import any names;
  • global const declarations;
  • __halt_compiler().

This means, the following script will work:

<?php
const x = 5;
echo x;

but the following will not compile:

<?php
{
  const x = 5;
  echo x;
}
查看更多
冷血范
4楼-- · 2019-01-09 09:54

Yes, but it won't create a new local scope. It's not something that would normally be done. Usually people mark blocks like this with comments.

Update:

It took a bit of digging to find a reference to it in the manual, but here it is:

http://www.php.net/manual/en/control-structures.intro.php

Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter.

The key here is statements can be grouped into a statement-group by encapsulating a group of statements with curly braces.

I also had a look for a reference to variable scope as it relates to this situation, but the manual doesn't specifically mention it, however you can think of it like this:

In PHP, functions and classes create a variable scope. You can read about that here. But a statement-group (as described above) does not. Don't think of the curly braces of a statement-group like the function (or class) wrapping brackets, but think of them like the curly braces that wrap the statement-group of control structures (if, for, while, switch, etc.) - because that's exactly what they are. It's clear that if you're using an if statement (or any other control structure) that the braces don't introduce a new scope, they are simply wrappers for a block of statements.

查看更多
甜甜的少女心
5楼-- · 2019-01-09 09:59

I also do this, solely because of my text editor (Komodo Edit 8.5). It's not a "bad reason" or "bad coding", if it helps you and doesn't cause any problems and if there's no other easy way to do it.

I solve the problem with a work-around:

if(1 == 1){ //always executing if function
//whatever you want to add
}
#

adding a # at the end prevents my editor from collapsing all empty lines below the curly brackets. This helps to further structure the code.

查看更多
登录 后发表回答