Ask a Question related to PHP Notes, Design and Development.
-
didou@php.net #1
note 24401 deleted from function.ob-get-contents by didou
Note Submitter: [email]drtdiggersNOSPAM@MAPSONhotmail.com[/email]
----
Finally found a spiffy use for ob_x functions! Alright, I had to write a query script that returned data in string form so that a Flash prog could easily query mySQL. Nothing special here. But, I was thinking, it would be nice to abstract queries like this in other PHP code as well, so that you could have some other existing script do the query, or whatever else you wanted it to do. So I came up with this:
// These functions allow PHP scripts to abstract functionality onto other scripts, not necissarily other PHP
// scripts thought...
// For example, abstracting database queries for simple information now adds an easily
// modifiable and maintainable security checkpoint. query.php can deal with
// checking for clearance and modules written that do queries can now call query.php
// to get the data, instead of having to manually execute a query statement
// in fact, will add functionality so that you can even flag it to just return the $result set if
// its a php script, or just let it return the query string and reassemble() it into whatever array
function executeExternal($script) {
list($script, $parameters) = explode('?', $script);
// Set up the parameters to the _GET string
$datasets = explode('&', $parameters);
foreach($datasets as $dataset) {
list($key, $value) = explode('=', $dataset);
$key = urldecode($key);
$value = urldecode($value);
$_GET[$key] = $value;
}
ob_start();
// Don't call it _directly_ from here, want all variables in this scope to be protected
$result = includethescript($script);
$value = ob_get_contents();
ob_end_clean();
// Depending on what was asked for, give it to them
if(isset($_GET['nostringreply']))
return $result;
else
return $value;
}
// Script CANNOT CANNOT CANNOT define any new functions and it will run in a protected scope
// basically doing exactly whats its saying its doing, executing the code
// in a clean scope, so don't mind if we trample these variables
function includethescript($__script) {
if(!substr_count($__script, '.php') && isset($_GET['nostringreply']))
die("can only request return of resource from a php script");
// $__resultset must be defined in the $__script if its been requested with nostringreply...
include($__script);
if(!isset($__resultset) && isset($_GET['nostringreply'])) {
// implicit flush
die("invalid php script for use with nostringreply option, did not return a resultset");
}
else
return $__resultset;
}
Note the restrictions placed on the foregin script if its php code: 1) the ability to straight request a Result Resource must be implemented in the script being called (play with $_GET['nostringreply'] to see if you should do this and set $__resultset if you should) 2) Cannot define functions within it. This is because in order to guarentee multiple calls, you're basically gluing your other PHP script into whatever is calling this function, and an include() is required, include_once() will only work for the first time. This provides a nice common abstraction layer for both a script-based(php or whatever, Perl if you want to be l33t ;) ) interface through PHP to a database and a Flash based interface through PHP to a database. Modifications to use $_POST would be trivial. Also note that nested output buffering works (I tried it) with this scheme, so you can maintain whatever output buffering deal you currently have going on.
didou@php.net Guest
-
note 33361 deleted from function.ob-start by didou
Note Submitter: digerati@n0spam.bellsouth.net ---- In respone to the comment by: daevid at daevid dot com about using include() with the... -
note 19929 deleted from function.is-a by didou
Note Submitter: php@electricsurfer.com ---- Here is a 3 line PHP implementation that worked for me: if (!function_exists('is_a')) {... -
note 21357 deleted from function.is-a by didou
Note Submitter: krisher@oswego.edu ---- Any implementation of is_a that relies on empty() to test whether the object parameter is empty will... -
note 19730 deleted from function.is-a by didou
Note Submitter: ernest@vogelsinger.at ---- For the previous example you need to exchange the function parameters to be compliant to the CVS... -
note 19727 deleted from function.is-a by didou
Note Submitter: dan at mojavelinux dot com ---- php implementation to keep us happy for now if (!function_exists('is_a')) { function...



Reply With Quote

