Solved

Use a Class's Method as the callback function for ob_start()

Posted on 2008-10-14
4
1,155 Views
Last Modified: 2012-05-05
How can I, or even may I, sue a class's method as the call back function for ob_start()?

I tried several ideas and googled, but nothing doing so far.

The output of the attached code returns:
--------------------------------------------------------------------------------------------
Hello World, and hello {testvar}
Goodbye World, and goodbye {testvar}
Hello World, and hello Beautiful
Goodbye World, and goodbye Ugly
--------------------------------------------------------------------------------------------

When I trying to get this result:
--------------------------------------------------------------------------------------------
Hello World, and hello Beautiful
Goodbye World, and goodbye Ugly
Hello World, and hello Beautiful
Goodbye World, and goodbye Ugly
--------------------------------------------------------------------------------------------

Thanks for the help!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Buffer Testing</title></head>
 

<body>

<?php

class Testclass {

	public $testvar;

	public $html;
 

	public function Testclass() {

		$this->testvar="unknown";

		$this->html="helloworld.html";

	}

	

	public function displayOutput() {

		include($this->html);

	}

	

	public function adjustOutput($buffer){

		return (str_replace("{testvar}", $testvar, $buffer));

	}

	

}//END class Testclass
 

$ONE= new Testclass();

$TWO= new Testclass();

$ONE->testvar="Beautiful";

$ONE->html="helloworld.html";

$TWO->testvar="Ugly";

$TWO->html="goodbyeworld.html";
 

function cheatOne($buffer){

	return (str_replace("{testvar}", "Beautiful", $buffer));

}

function cheatTwo($buffer){

	return (str_replace("{testvar}", "Ugly", $buffer));

}
 

ob_start();//START MAIN BUFFER
 

  ob_start("$ONE->adjustOutput");//buffer one

    $ONE->displayOutput();

  ob_end_flush();

  

  ob_start("$TWO->adjustOutput");//buffer two

    $TWO->displayOutput();

  ob_end_flush();
 

  ob_start("cheatOne");//CHEATING buffer one

    $ONE->displayOutput();

  ob_end_flush();

  

  ob_start("cheatTwo");//CHEATING buffer two

    $TWO->displayOutput();

  ob_end_flush();
 

ob_end_flush();//OUTPUT THE MAIN BUFFER
 

?>

</body>

</html>

Open in new window

0
Comment
Question by:dban00b
  • 2
  • 2
4 Comments
 
LVL 1

Author Comment

by:dban00b
ID: 22716543
There's a typo in Testclass::adjustOutput()  "$testvar"  should read "$this->testvar" -- no change in the original question though, just didn't want anyone to get hung up on that typo.
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 500 total points
ID: 22719629
This is a common thing. instead of providing the name of the function to call, you pass an array containing the instance and the function name, like this:

ob_start(array($ONE, 'adjustOutput');

Docs are here: http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 22719631
Oops, missed a bracket:

ob_start(array($ONE, 'adjustOutput'));
0
 
LVL 1

Author Comment

by:dban00b
ID: 22722001
Awesome!  Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
phpmyadmin 3 32
Could you possibly give a help on how to configure Datatables plugin to make it correctly runs? 5 37
mysqli 3 18
PHP preg_replace code convert to Delphi 14 32
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now