在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
来源:http://stackoverflow.com/questions/9705281/with-and-let-in-php
use(&$a) 用 use ($parameter) 这种语法来往一个函数里面传参。比如往一些回调函数里面传参,这是医用手段。 我们再继续深入一下 PHP中的 anonymous function , 体会一下 use 的用法 和 有 & 符号与没有 $ 符号的区别。
Recently I found out about I'm interested in achieving something like mentioned here:http://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? Answer: first: 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 As for second: PHP doesn't have 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;
Finally, we call We then call |
2022-08-16
2022-08-30
2022-07-18
2022-08-18
2022-07-29
请发表评论