classes into session variables
Moderator: General Moderators
-
patnet2004
- Forum Newbie
- Posts: 14
- Joined: Sat Jul 19, 2003 3:26 am
- Location: Computer Desk
classes into session variables
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
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
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
I think patnet2004 means that this doesn't work.......
INDEX.PHP
INCLUDE.PHP
INDEX.PHP
Code: Select all
<?
class Test
{
function DoSomething()
{
echo "Something";
}
}
$xxx = new Test():
require_once("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).Gen-ik wrote:I think patnet2004 means that this doesn't work.......
INDEX.PHPINCLUDE.PHPCode: Select all
<? class Test { function DoSomething() { echo "Something"; } } $xxx = new Test(): require_once("include.php"); ?>Code: Select all
<? $xxx -> DoSomething(); ?>
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
oh well i may have just been trippin...
ya, what i was trying to do is:
Index.php
class.php
use_class.php
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
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);
?>Code: Select all
<?php
class myClass
{
function hello()
{
echo"hello";
}
}
?>Code: Select all
<?php
$myClass->hello();
?>i may have called the wrong file in the browser ("use_class.php instead of index.php")
oh well my mistake, it was late
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
i think i found a way of fixing that self inflicted error...
heres what i made:
index.php
class.php
useClass.php
heres what i made:
index.php
Code: Select all
<?php
class index
{
var $enabled = NULL;
function index()
{
$this->enabled = 1;
}
}
$index = new index();
require_once("class.php");
$myClass = new myClass;
require_once("useClass.php");
?>Code: Select all
<?php
if(!isset($index->enabled))
{
die("You didn't start at index.php");
}
class myClass
{
function hello()
{
echo"hello";
}
}
?>Code: Select all
<?php
if(!isset($index->enabled))
{
die("You didn't start at index.php");
}
$myClass->hello();
?>