classes into session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
patnet2004
Forum Newbie
Posts: 14
Joined: Sat Jul 19, 2003 3:26 am
Location: Computer Desk

classes into session variables

Post by patnet2004 »

i just now got to class in my php book and because the instance of the class is stored in variables you lose them when you go into a include file so i was wondering if it was a good idea or not to store them in session variables?
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

how do you explain you lose them? i don't think thats true...

anyways, if u want to save an object(class) u use the serialize() function which takes a variable as parameter and returns a serialized string which can be then passed thru URLs or session variables or whatever you may think of...

to unserialize, just use unserialize() and the parameter will be the serialized string...

good luck
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I think patnet2004 means that this doesn't work.......

INDEX.PHP

Code: Select all

<?
class Test
{
    function DoSomething()
    {
        echo "Something";
    }
}

$xxx = new Test():

require_once("include.php");

?>
INCLUDE.PHP

Code: Select all

<?
    $xxx -> DoSomething();
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Gen-ik wrote:I think patnet2004 means that this doesn't work.......

INDEX.PHP

Code: Select all

<?
class Test
{
    function DoSomething()
    {
        echo "Something";
    }
}

$xxx = new Test():

require_once("include.php");

?>
INCLUDE.PHP

Code: Select all

<?
    $xxx -> DoSomething();
?>
Actually that will work just fine. An included file is basically just another block of the current script (except an include initially jumps out of php - hence the need for php tags if you include a script file).

Any vars in the scope of the include line are available in the included script.

Can you give us some more information about your problem patnet2004?
Last edited by McGruff on Wed Aug 10, 2005 12:04 pm, edited 1 time in total.
patnet2004
Forum Newbie
Posts: 14
Joined: Sat Jul 19, 2003 3:26 am
Location: Computer Desk

Post by patnet2004 »

oh well i may have just been trippin... :oops:
ya, what i was trying to do is:
Index.php

Code: Select all

<?php
require_once("class.php");
$myClass = new myClass;
require_once("use_class.php);
?>
class.php

Code: Select all

<?php
class myClass
&#123;
	function hello()
	&#123;
		echo"hello";
	&#125;
&#125;
?>
use_class.php

Code: Select all

<?php
$myClass->hello();
?>
but i just tested it again and it works :?
i may have called the wrong file in the browser ("use_class.php instead of index.php")
oh well my mistake, it was late :lol:
Last edited by patnet2004 on Sat Nov 01, 2003 6:30 pm, edited 2 times in total.
patnet2004
Forum Newbie
Posts: 14
Joined: Sat Jul 19, 2003 3:26 am
Location: Computer Desk

Post by patnet2004 »

i think i found a way of fixing that self inflicted error...
heres what i made:
index.php

Code: Select all

<?php
class index
&#123;
	var $enabled = NULL;
	function index()
	&#123;
		$this->enabled = 1;
	&#125;
&#125;
$index = new index();

require_once("class.php");
$myClass = new myClass;
require_once("useClass.php");
?>
class.php

Code: Select all

<?php
if(!isset($index->enabled))
&#123;
	die("You didn't start at index.php");
&#125;

class myClass
&#123;	
	function hello()
	&#123;
		echo"hello";
	&#125;
&#125;
?>
useClass.php

Code: Select all

<?php
if(!isset($index->enabled))
&#123;
	die("You didn't start at index.php");
&#125;
$myClass->hello();
?>
Post Reply