...

View Full Version : assigned an object to session in class and reloading, stupid or possible?



Exodious
11-20-2003, 10:32 PM
<?
session_start();
header("Cache-control: private");
?>
<?php include("pageControl.php") ?>

<?php
# baseclass to test ability to grab the session objects
$pageInitObj = new pageInitialisation();

class pageInitialisation
{
var $pageDefObj;

function pageInitialisation()
{
print "<br><b>New instantiation of pageInitialisation</b>";
print "<br><br>session has? - " . $_SESSION["pageDefObj"];

if(isset($_SESSION["pageDefObj"]))
{
print "<br>session has object of type pageDefObj so setting pageInitialisation pageDefObj to session obj";

# if exists, get ref to and so become
$this->$pageDefObj =& $_SESSION["pageDefObj"];
$this->$pageDefObj->printSomething();
}
else
{
print "<br>session for obj pageDefObj is empty";
# create new obj of type pageControl, then assign to session obj
print "<br>Setting pageDefObj now<br>";

$this->pageDefObj = new pageControl();

$_SESSION["pageDefObj"] =& $this->$pageDefObj;
print "<br>added obj to session";
}
}
}

?>


Hiya, I am trying to work out if the global $_SESSION variable can be used as I've tried to above.

I want to load one class, then reload an object from the session and assign it to a variable in that class (which is a reference to another class)

Essentially meaning one class call is all thats needed to setup the page, work out details, do db loads (or use already loaded) etc...

Thx for comments

firepages
11-21-2003, 06:44 PM
yes , as long as the class definition is available (eg included ) thats fine ... e.g



<?
session_start();
include_once 'bar.class.php';

class foo{
function foo(){
$this->bar = ( isset( $_SESSION['bar'] ) ) ? $_SESSION['bar'] : new bar() ;
print_r( $this->bar );
}
}

foo::foo();
?>


where $_SESSION['bar'] is an instance of class bar() from bar.class.php;

Exodious
11-22-2003, 09:55 PM
<?php

class pageInitialisation
{
var $pageDefObj;

function pageInitialisation()
{
print "<br><b>New instantiation of pageInitialisation</b>";
print "<br><br>session has? - " . $_SESSION["pageDefObj"];

$this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : $this->pageDefObj =& new pageControl();

if(isset($_SESSION["pageDefObj"]))
{
print "<br>session has object of type pageDefObj so setting pageInitialisation pageDefObj to session obj";

# if exists, get ref to and so become
#$this->pageDefObj =& $_SESSION["pageDefObj"];
$this->pageDefObj->printSomething();
}
else
{
print "<br>session for obj pageDefObj is empty";
# create new obj of type pageControl, then assign to session obj
print "<br>Setting pageDefObj now<br>";

#$this->pageDefObj = new pageControl();

#$_SESSION["pageDefObj"] =& $this->$pageDefObj;
print "<br>added obj to session";
}

$_SESSION['pageDefObj'] =& $this->pageDefObj;

}

}

# baseclass to test ability to grab the session objects
#$pageInitObj = new pageInitialisation();
pageInitialisation::pageInitialisation();
?>


What am i doing wrong? The page I am using has 3 buttons (in form) directed to index.php?id=1/2 & 3, but once clicked, I get error executing function as if the object doesnt really exist...?

Thx for comments

p.s. that function if you need to know, is the call to $this->pageDefObj->printSomething();

firepages
11-23-2003, 02:53 AM
Hi , try this as a diagnostic , without having the included classes is hard for me to guess whats happening ..but often dereferencing the object can help ... eg

$obj = $this->object;
$obj->do_something();

see what this outputs .. if the method call does not work it should dump the object



<?php
class pageInitialisation{
var $pageDefObj;
function pageInitialisation(){
print "<br><b>New instantiation of pageInitialisation</b>";
print "<br><br>session has? - " . $_SESSION["pageDefObj"];

$this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : new pageControl() ;

$obj=$this->pageDefObj;

if( method_exists( $obj ,'printSomething' ) ){
$obj->printSomething();
}else{
echo '<pre>';
print_r( get_class_methods( $obj ) );
var_export( $obj ) ;
echo '</pre>';
}

$_SESSION['pageDefObj'] =& $this->pageDefObj;
}
}
pageInitialisation::pageInitialisation();
?>

Exodious
11-24-2003, 01:47 PM
Hiya, ran the code and outputted:



Array
(
)
array (
'__PHP_Incomplete_Class_Name' => 'pagecontrol',
)





class pageControl
{

function pageControl()
{
print "<br><b>PAGECONTROL - New instantiation of pageControl</b>";
}

function printSomething()
{
print "<br>Something printed<br>";
}
}


The pageControl class is only a test atm, but later on tonight I'll be building it (its in my self-employed state which == after work)

Thx for comments

firepages
11-24-2003, 04:27 PM
Hi , the error you post is what I would expect when running the code above without having the pageControl class available , eg (and I forgot to put this in my example so apologies if this is the issue..)

<?php
include ('pageControl.class.php');

//rest of code
?>

eg without the class definition the object can not be reconstructed.

Exodious
11-24-2003, 06:13 PM
Hi, well, heres the whole page including my commented out class, the definition is definetely included, I was wondering if it was something to do with passing a reference to the object into the session



<?
session_start();
header("Cache-control: private");
?>
<?php include("pageControl.php") ?>

<?php
/*
class pageInitialisation
{
var $pageDefObj;

function pageInitialisation()
{
print "<br><b>New instantiation of pageInitialisation</b>";
print "<br><br>session has? - " . $_SESSION["pageDefObj"];

$this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : $this->$pageDefObj =& new pageControl();

if(isset($_SESSION["pageDefObj"]))
{
print "<br>session has object of type pageDefObj so setting pageInitialisation pageDefObj to session obj";

# if exists, get ref to and so become
#$this->pageDefObj =& $_SESSION["pageDefObj"];
$obj = $this->pageDefObj;
$obj->printSomething();
}
else
{
print "<br>session for obj pageDefObj is empty";
# create new obj of type pageControl, then assign to session obj
print "<br>Setting pageDefObj now<br>";

#$this->pageDefObj = new pageControl();

#$_SESSION["pageDefObj"] =& $this->$pageDefObj;
print "<br>added obj to session";
}

$_SESSION['pageDefObj'] =& $this->$pageDefObj;

}

}*/

class pageInitialisation{
var $pageDefObj;
function pageInitialisation(){
print "<br><b>New instantiation of pageInitialisation</b>";
print "<br><br>session has? - " . $_SESSION["pageDefObj"];

$this->pageDefObj = ( isset( $_SESSION['pageDefObj'] ) ) ? $_SESSION['pageDefObj'] : new pageControl() ;

$obj=$this->pageDefObj;

if( method_exists( $obj ,'printSomething' ) ){
$obj->printSomething();
}else{
echo '<pre>';
print_r( get_class_methods( $obj ) );
var_export( $obj ) ;
echo '</pre>';
}

$_SESSION['pageDefObj'] =& $this->pageDefObj;
}
}


# baseclass to test ability to grab the session objects
#$pageInitObj = new pageInitialisation();
pageInitialisation::pageInitialisation();
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum