1

I prefer to write html outside of php tags, so eclipse can display the html with proper syntax highlighting. The concatenation method is harder to write and doesn't highlight (see the two examples below).

I apologize for the length. These examples are very simple, so it should be an easy read.

I DON'T like this, too many 'quotes' and $o's, and no syntax highlighting!:

<?php

display($something){
    $o = '';
    $o .= '<div>';
    $o .=     $something;
    $o .= '</div>';
    return $o;
}

// I want to be able to do this:    
echo display(display('something'));

This gives a function the chance to finish the closing <tag> or even add additional html afterwards. The above example is functionally what I'm looking to do, but for these reasons ('quotes', $o's, and syntax highlighting) I haven't created a system like this.

The following example is how I prefer to write html, but I can't nest output, because it doesn't return!

<?php

function display($something){ ?>
    <div>
        <?=$something?>
    </div>
<?php }

// I'd like to do this, but I can't  
display(display('this doesn't return anything to the first function call...'));

This is where output buffering comes in, I'll get back to that in a second...

What I'm envisioning:

I'd like to be able to use func_get_args() to accomplish something like this (note, this will apply to OOP objects, just keeping it simple here):

<?php

some_panel( title_style_1('Sample Header'),
            panel_content(you_tube($vid_id)),
            small_img_frame($img_src) );

You'd basically be able to take any of these output functions and nest them any way you like. Just like you can put any <div> inside any <p> and vice versa. Only problem is, you have to make sure you close the tags... And, in this case, you could add any markup at the end or in between children.

This is where the output buffering comes in

<?php

function display($something){ 
    ob_start(); // <---- Start buffer ?>
    <div>
        <?=$something?>
    </div>
<?php return ob_end_clean(); // <------ Return output
}

// Now I can do this!!!
echo display(display('this should work!'));

And, drum roll please.... THE QUESTION:

If I'm repeatedly buffering potentially hundreds or even thousands of times per request, is this going to be a performance hit? I've read posts that warn against output buffering due to:

Reliability: If somewhere else a buffer was started, from what I read, it sounds like these nest and can potentially conflict.

Maintainability: If a buffer is started, you have to guarantee it will be stopped.

For my system, if output buffering is started and stopped in the same function call, these things seem to be OK. It's the excessive iteration of potentially 1000's of items that each start/stop output buffering for a single <li> that I'm worried about.

Also, if anyone knows of any frameworks or better ways to do what I'm trying to do, any suggestions would be appreciated!

Michael Lewis
  • 4,252
  • 6
  • 28
  • 39
  • sorry i cant answer really. but if your doing such things, did you think about using smarty or twig? i love the power you have there. and you can still be "view" driven if you want to. – Julian Hille Feb 28 '13 at 01:26
  • I'm not familiar with what these bring to the table. My initial impression (a while ago) was that they weren't necessary, or didn't really help much. I'll look into those though, thank you! – Michael Lewis Feb 28 '13 at 01:31
  • 2
    i would suggest twig, but thats only an opinion. You could inerhit there. and have at any part code generated and inject. also you can have "blocks" of contents which you prepaned append fill etc. i really like it. – Julian Hille Feb 28 '13 at 01:34
  • 1
    I agree on the Twig suggestion, I use Symfony2 which uses Twig as its default template language and it's really nice. – Peter Wooster Feb 28 '13 at 01:40

2 Answers2

0

How about nesting output via… output?

<?php
function foo($itemName) {
?>
    <div class="item">
        <?php bar($itemName); ?>
    </div>
<?php
}

function bar($itemName) {
?>
    <h1><?= $itemName ?></h1>

    <p>Hello, world!</p>
<?php
}
?>

But to answer the rest of the question: benchmark it! Output buffering is usually not a problem, but it could very well be anybody’s bottleneck. It depends.

If you find yourself doing this sort of thing a lot, consider breaking it out in to several files or using a template system. Or not PHP.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • As the complexity of the output grows, I'd need to be able to pass a bunch of parameters through foo() into any number of sub-output functions. This would be tricky, and I'm looking to be able to 'configure' this output from the top level (same scope as foo())... If that makes sense. :-\ – Michael Lewis Feb 28 '13 at 01:30
  • @MikeLewis: Pass one array around, maybe? (PHP’s arrays are copy-on-write, too.) – Ry- Feb 28 '13 at 01:37
0

Output buffering is probably a wash, it may even improve performance. The CPU wasted buffering is saved in doing less I/O. Socket writes are actually thousands of instructions. The only time it could become a problem is when the amount of output would adversely impact memory usage. And if you are buffering many megabytes of output you probably need to look into some form of streaming.

Here's an older post on this topic PHP output buffering - sounds like a bad idea, is it?

Community
  • 1
  • 1
Peter Wooster
  • 6,009
  • 2
  • 27
  • 39