Page 1 of 1

Q: Call to another PHP page into a variable (no echo)

Posted: Mon Mar 10, 2003 1:46 pm
by Haryon
Is it possible with PHP, to have a file1.php call for a file2.php and get the result into a variable and not displayed to the 'browser' (not directly echoed) ?

What I want to do ?
- transforming XML files into HTML, where the XML file is generated by a PHP code and uses parameters from the request or stored in session (like the user which is connected, to be able to check for rights, or to retreive his data from the db)
- processing (translating) a secondary php page

Java/Servlet :
- basically in JSP/Servlet you have the request coming to your servlet (or jsp, jsps are servlets anyway), you can do code, then "redirect" (RequestDispatcher.include()) the request to another servlet, the servlet can generate stuffs and put them into a new variable into the request (as attribute : request.setAttribute()) or in the session, return to the original servlet, which does some final code, and return a result to the browser. All of this without leaving the engine.

Basically it looks like :

- User fills a form and presses Submit
-- request goes to my web server and php engine
-- main.php (the application navigation layer, more or less) receives the request
----- main.php calls for subquery.php
----- subquery.php can use the data from the form the user filled, it can also check his cookies (session, check whether the user is connected, whether he has the rights to see that specific subquery page, retrieves his data from the database, etc.)
----- subquery.php build a data XML file and ends
-- main.php receives the result of subquery.php and process it in some way (like call for a XSLT)
-- main.php echoes (as the echo() php command, that is it writes the resulting document back into a response to the browser) the result
- the user sees his nice shiny HTML page

Summary :
- how to redirect to another php page from a given page, and keeping access to the original request (so I guess no header("Location:") since this writes back to the browser) ?
- how to recover the result of some php file into something else than the response to the browser ? (if possible I would like to still be able to fastly type some html code in the 'subquery.php' but main.php captures it).

if you have any Pointers, I thank you very much.


Why ?
- I want to design my application presentation layer in some evolutive, flexible, generic way. using something similar to what they call Model2 in Java, and which is nothing else than MVC pattern.

Posted: Mon Mar 10, 2003 7:00 pm
by volka
a short example of what (/something that) is possible

Code: Select all

<?php

class MyOutput
{
	function doOutput() { }
}

$GLOABLS['output'] = 'result: ';
$result = require('process2.php'); // includes another script (almost) as the code would stand here

somethingToCall();
echo somethingElseToCall();
echo $GLOABLS['output'], '<br />';
if (is_object($result) AND method_exists($result, 'doOutput'))
	$result->doOutput();

?>

Code: Select all

<?php // process2.php
echo 'this is processed immediately<br />';
$GLOABLS['output'] .= 'the superglobal array GLOABLS is available anywhere';

function somethingToCall()
{
	echo 'somethingToCall(): processed when called<br />';
}

function somethingElseToCall()
{
	return 'result of somethingElseToCall()<br />';
}

class Process2output extends MyOutput
{
	var $myText;
	
	function doOutput()
	{
		echo 'Process2output: ', $this->myText;
	}
}

$p = new Process2output;
$p->myText = 'just a test';

return $p;
?>
hope it helps