0

Let's say in PHP you have a class Parent:

<?php
class Parent {

    public function speak()
    {
        // Do bunch of stuff
        $output = outcomeOfAbove();
        echo $output;
    }

}

And you have a class Child:

<?php
class Child extends Parent{

    public function speak()
    {
        parent::speak();

        echo 'Hello from child';
    }

}

Now let's say that I want the content of $output in the Child speak() function, is there a way to capture the output of parent::speak(); and stop this function from echoing? Knowing that you can't change the 'Parent' class.

Crinsane
  • 818
  • 3
  • 16
  • 28
  • 2
    ob_* functions; but Child needs to extend Parent; and typically it's better for methods to return a value rather than echo a value – Mark Baker Mar 31 '13 at 10:12
  • Yeah, forgot the extend part, updated it :) And also forget to add "knowing that you can't change the 'Parent' class." – Crinsane Mar 31 '13 at 10:34

1 Answers1

1

You can use the PHP output buffering feature:

  public function speak()
  {
    ob_start();
    parent::speak();
    $parent_output = ob_get_contents();
    ob_end_clean();

    echo 'Hello from child';
  }

}

For more info, see the PHP.net manual.

Good luck ;)

achedeuzot
  • 4,164
  • 4
  • 41
  • 56
  • That did the trick! I knew it was something with ob_* functions, but I used `ob_get_flush();` Thanks! – Crinsane Mar 31 '13 at 10:37