Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

User defined objects?
Welcome, Guest. Please login or register.
December 02, 2008, 04:20:35 AM
11304 Posts in 1248 Topics by 498 Members
Latest Member: katCheeme
Experts Round Table Network  |  Serverside Technology  |  PHP  |  User defined objects? « previous next »
Pages: [1]
Author Topic: User defined objects?  (Read 314 times)
NeoTeq

Offline Offline

Posts: 21


« on: June 05, 2006, 04:11:57 AM »

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

Code:
<?

$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

Code:
<?

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

Code:
<?

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
Logged

Still claiming: There is no peace.
NeoTeq

Offline Offline

Posts: 21


« Reply #1 on: June 05, 2006, 12:32:01 PM »

Solution...

udc.php
Code:
<?

$UDCONTROL_PREFIX = "UDControl";

session_start();

if (!isset($_SESSION[$UDCONTROL_PREFIX])) {
$_SESSION[$UDCONTROL_PREFIX] = Array();
}

foreach ($_SESSION[$UDCONTROL_PREFIX] as $Key => $Value) {
if ($Value->getPageUri() != $_SERVER["SCRIPT_NAME"]) {
unset($_SESSION[$UDCONTROL_PREFIX][$Key]);
echo $Key;
}
}

class UDControl {

var $ObjectName = "";
var $PageUri = "";
var $Source = "";

function UDControl($strObjectName, $strPageUri) {
$this->setObjectName($strObjectName);
$this->setPageUri($strPageUri);
}

function getObject($strObjectName, $strPageUri) {
global $UDCONTROL_PREFIX;

if (isset($_SESSION[$UDCONTROL_PREFIX][$strObjectName])) {
$UDControl = $_SESSION[$UDCONTROL_PREFIX][$strObjectName];
$UDControl->setPageURI($strPageUri);
}
else {
$UDControl = new UDControl($strObjectName, $strPageUri);
}

return $UDControl;
}

function setSource($strSource) { $this->Source = $strSource; }
function getSource() { return $this->Source; }

function setObjectName($strObjectName) { $this->ObjectName = $strObjectName; }
function getObjectName() { return $this->ObjectName; }

function setPageUri($strPageUri) { $this->PageUri = $strPageUri; }
function getPageUri() { return $this->PageUri; }

function Render() {
global $UDCONTROL_PREFIX;

$_SESSION[$UDCONTROL_PREFIX][$this->getObjectName()] = $this;

return $this->getSource();
}

}

?>
Logged

Still claiming: There is no peace.
Pages: [1]
« previous next »
    Jump to: