0

Recently I found out about with and let statements for javascript.

I'm interested in achieving something like mentioned here: https://stackoverflow.com/a/1462102/393406, except for PHP in OOP fashion.

Like:

$builder = new markupbuilder();
with($markupbuilder){
    div(
        h1('A title...') . 
        p('This is ' . span('paragraph') . ' here.')
    );
}

// respectively
$builder->div(
    $builder->h1('A title...') . 
    $builder->p('This is' . $builder->span('paragraph') . ' here')
);

So, is there something similar for PHP or how could I achieve this in PHP?

Community
  • 1
  • 1
tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • No, these language constructs do not exist in PHP. As you haven't posted any code, it's hard to make suggestions what's fitting instead. So why not add some more specific things to your question? – hakre Mar 14 '12 at 15:52
  • 1
    anyway... avoid using "with" http://www.2ality.com/2011/06/with-statement.html – pleasedontbelong Mar 14 '12 at 15:56

3 Answers3

2

No, there's no with or let statement in php.
But the alternative mentioned in "with Statement Considered Harmful", April 11, 2006 at 7:52 am by Douglas Crockford works with php, too.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
2

This is sort of a trite reply, but you have not saved a single character of typing in your example, and I believe that's the whole point of using with. The with statement also introduces ambiguity. Do you want to call getElementsByTagName on the DOMDocument object, or do you want to call the globally defined function of the same name?

As for let, PHP variables apply to the scope they are in by default -- you have to use the global keyword not only to make them globally accessible in general but any time you want to use them in another scope (and I would suggest never to use them period). Some may say it is a limitation on PHP that if and other constructs don't have their own scope, and let may be useful there, but if you wanted another variable in one of those constructs you could just declare it separately or move the functionality to a function scope.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

PHP doesn't have let but you can accomplish the same thing using other constructs, thought it is much messier!

First, what we wish we could do: (This doesn't work)

echo '<pre>';
$a = 5;
for (let $i=0; $i<10; $i++) {
    var_dump($i*$a);
}
var_dump('done looping');
var_dump(isset($i));
exit;

This is what we have to do instead:

echo '<pre>';
$a = 5;
$loop = function() use (&$a) {
    for ($i=0; $i<10; $i++) {
        var_dump($i*$a);
    }
};
$loop();
var_dump('done looping');
var_dump(isset($i));
exit;

$loop is assigned to an anonymous function which is actually a closure. Because $i is defined inside the function, it now has function scope and can't "leak". $a on the other hand could be passed to the function as a param, but this would get in the way if you wanted this function to have real parameters. Instead we give the function access to $a through the use statement.

Finally, we call $loop(); because PHP doesn't let us call anonymous functions the way JavaScript does function() { alert('My anonymous function!'); }();

We then call var_dump(isset($i)); and see that $i is not set outside of the function.

Tyler V.
  • 2,471
  • 21
  • 44