Howdy good folks of ERT!
I'm in an experimenting mood (this being a holiday and me being out of beer), so I decided to try and create a user-defined object in php. Now, to complicate things a bit, I want the state of the object at the time of rendering (it'll be some sort of html table in the end, based on a query or recordset) to be saved to the session. That works good I think. But what doesn't work is getting the object from the session back to my object.
Edit: I'm sorry, but the script had a few more problems, mainly with variable scope. I've also added something to unset unnecesary objects, and split the thing into three files for testing.udc.php<?
$UDCONTROL_PREFIX = "UDControl";
session_start();
if (!isset($_SESSION[$UDCONTROL_PREFIX])) {
$_SESSION[$UDCONTROL_PREFIX] = Array();
}
foreach ($_SESSION[$UDCONTROL_PREFIX] as $Key => $Value) {
if ($Value->getPage() != $_SERVER["SCRIPT_NAME"]) {
unset($_SESSION[$UDCONTROL_PREFIX][$Key]);
}
}
class UDControl {
function UDControl($strObjectName, $strPage) {
global $UDCONTROL_PREFIX;
$this->setObjectName($strObjectName);
$this->setPage($strPage);
if (isset($_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()])) {
$this = $_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()];
}
}
function setSource($strSource) { $this->Source = $strSource; }
function getSource() { return $this->Source; }
function setObjectName($strObjectName) { $this->ObjectName = $strObjectName; }
function getObjectName() { return $this->ObjectName; }
function setPage($strPage) { $this->Page = $strPage; }
function getPage() { return $this->Page; }
function Render() {
global $UDCONTROL_PREFIX;
$_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()] = $this;
return $this->getSource();
}
}
?>
test.php<?
include("udc.php");
$myObject = new UDControl("myobject", "/test/test.php");
$myObject->setSource("SELECT * FROM myTable");
echo $myObject->Render();
$myObject2 = new UDControl("myobject2", "/test/test.php");
$myObject2->setSource("SELECT ID FROM this");
echo $myObject2->Render();
?>
index.php<?
include("udc.php");
?>
The error still occurs on the same line, in the object declaration when I try to copy the object from session back to the page.
Fatal error: Cannot re-assign $this in ****\test\udc.php on line 26