Incomplete class obj when setting object as session instance

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
Wicky
Forum Newbie
Posts: 2
Joined: Sun May 07, 2006 8:06 am

Incomplete class obj when setting object as session instance

Post by Wicky »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I wrote a code something like this

Code: Select all

class cart
{
  var $test;
  function cart
 {
     $this->test = "This is a session object variable";
 }
}
This class is written in a file called class.inc and i use it in a page called test.php like so:

Code: Select all

require_once "cart.inc";
$obj = new cart;
session_register("obj");
Then i tried to print the object on another page, called it default.php

Code: Select all

require_once "cart.inc";
echo $obj->test;
the problem is it doesnt print, it prints only if i copy and paste the code in the default.php page. I already wrote a function _autoload to load it, but it doesnt help, tried changing the extension of the files to class.cart.php and to the rest, it doesnt help and it doesnt print. ONLY PRINTS WHEN I COPY AND PASTE THE CODE INTO THE SAME PAGE!!!!!!! CAN ANYONE PLEASE HELP :( i am desperate, got stuck somewhere i shouldnt be..........thznk


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Run the following in a new file and tell us the results please.

Code: Select all

<?php

$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
$rg = (in_array(strtolower(ini_get('register_globals')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$de = (in_array(strtolower(ini_get('display_errors')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$so = (in_array(strtolower(ini_get('short_open_tag')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$eol = (isset($_SERVER['HTTP_HOST']) ? "<br />\n" : "\n");

$ec = array(
   'E_STRICT' => 2048,
   'E_ALL' => 2047,
   'E_USER_NOTICE' => 1024,
   'E_USER_WARNING' => 512,
   'E_USER_ERROR' => 256,
   'E_COMPILE_WARNING' => 128,
   'E_COMPILE_ERROR' => 64,
   'E_CORE_WARNING' => 32,
   'E_CORE_ERROR' => 16,
   'E_NOTICE' => 8,
   'E_PARSE' => 4,
   'E_WARNING' => 2,
   'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
   if (($t & $v) == $v)
   {
      $e[] = $n;
      $t ^= $v;
   }
}
$er = $er . ' (' . implode(' | ', $e) . ')';

echo 'PHP Version: ' . $ve . $eol;
echo 'PHP OS: ' . $os . $eol;
echo 'Error Reporting: ' . $er . $eol;
echo 'Register Globals: ' . $rg . $eol;
echo 'Short Tags: ' . $so . $eol;
echo 'Display Errors: ' . $de . $eol;

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

For current versions of PHP is should be something like this to save the object:

Code: Select all

require_once "cart.inc";
$cart = new cart();
session_start();
$cart->test = 'Saved in session.';
$_SESSION['cart'] = $cart;
And retrieve the object

Code: Select all

require_once "cart.inc";
session_start();
$cart = $_SESSION['cart'];
echo $cart->test;
Note that the order of these statements is very important.
(#10850)
Wicky
Forum Newbie
Posts: 2
Joined: Sun May 07, 2006 8:06 am

Post by Wicky »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


PHP Version: 5.1.0RC1
PHP OS: WINNT
Error Reporting: 2039 (E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_WARNING | E_ERROR)
Register Globals: On
Short Tags: On
Display Errors: On

Thats the result i get from running the code you gave me....

For the second reply, i tried that method and it dint work, i will paste my chunk of code to handle the cart for you to see.......Trying to implement it as a class at the first stage but ran into these problems. I dunno if i am using a right way by having arrays in class or shouldd i use another data structure to handle it ... Please advise, masters of php

Code: Select all

<?php 
session_start();
	class cart
	{
		var $cartItem = array();
		var $label_cartItem = array();
		var $combine_array = array();
		var $test;
		function cart()
		{
			$this->test = "session testing";
		}
		function initializeCart($numberOfItems)
		{
			for($i=0;$i<$numberOfItems;$i++)
			{
				$this->cartItem[$i]= array();
			}
		}
		function combine_cart_array($label_cartItem,$value_arrays)
{
	$this->label_cartItem = $label_cartItem;
	
	if(is_array($this->label_cartItem) && is_array($value_arrays))
	{
		
		$combine = array();
		$count = count($value_arrays);
		$j = 0;
		foreach ( $this->label_cartItem as $value)
		{
				$this->combine_array[$value] = $this->cartItem[$j];
				$j++;
		}
		//return $combine;
	}
	else
	{
		return false;
	}
}
	}
	$username = $_SESSION['username'];
	//Declare cart array
	if ( !isset($_SESSION['cartInfo']))
	{
		{
			$_SESSION['cartInfo'] = array();
			$_SESSION['cartCounter'] = 0 ;
		}
	}
	else
	{	
		if(isset($HTTP_POST_VARS['cartInit']) && $HTTP_POST_VARS['cartInit']="true" )
		{
			unset($HTTP_POST_VARS['cartInit']);
			$counter = $_SESSION['cartCounter'];
			$cartObject= new cart();
			$cartObject->initializeCart(8);
			$cartObject->label_cartItem = array("cartCategory","cartDescription","cartPrice","fabricInfo","styleInfo","galleryInfo");
			$cartObject->combine_cart_array(($cartObject->label_cartItem),($cartObject->cartItem));
			$_SESSION['cartInfo'][$counter] = $cartObject;
			//session_register("cartObject");
			
			$counter = $counter+1;
			$_SESSION['cartCounter'] = $counter;
		}
		if(isset($HTTP_POST_VARS['cartItemName']) && $HTTP_POST_VARS['cartItemName']="Style")
		{
			unset($HTTP_POST_VARS['cartItemName']);
			$styleId2 = $HTTP_POST_VARS['styleId'];
			$styleId = array();
			$where = " styleId = '$styleId2'";
			$styleName = array();
			$description = array();
			$styleCode1 = array();
			$category = array();
			$styleCode2 = array();
			$image = array();
			$styleId = $dbfunctions->db_values_array("style","styleId","","styleId",$where);
			$styleName =  $dbfunctions->db_values_array("style","styleName","","styleId",$where);
			$styleCode1 =  $dbfunctions->db_values_array("style","styleCode1","","styleId","$where");
			$styleCode2 =  $dbfunctions->db_values_array("style","styleCode2","","styleId","$where");
			$description =  $dbfunctions->db_values_array("style","description","","styleId",$where);
			$category =  $dbfunctions->db_values_array("style","category","","styleId",$where);
			$image =  $dbfunctions->db_values_array("style","image","","styleId","$where");
			$styleInfo = array();
			$styleInfo = $dbfunctions->combine_db_arrays(array("styleId","styleName","styleCode1","styleCode2","description","category","image"), array(0=>$styleId,1=>$styleName,2=>$styleCode1,3=>$styleCode2,4=>$description,5=>$category,6=>$image));
			$counter = $_SESSION['cartCounter'] - 1;
			$_SESSION['cartInfo'][$counter]->combine_array['styleInfo']= $styleInfo;
			
		}
						
	}
			
				
										$counter = $_SESSION['cartCounter'] - 1;
										echo $counter;
										echo $_SESSION['cartInfo'][$counter]->combine_array['styleInfo']['styleName'][0];
										 //$cartObject->test;
?>

The wierd thing about this code is that, if i take out the if($HTTP_POST_VARS) and if(isset($_SESSION['cartObject]) statement everything seems to be working fine when i view the cart, but if i put even one of it back then nothing shows, seems like the object disappears or something. Also this happens only when i try to show the information on the same page, i.e. cart.php, by clicking to other links and back to this one. But if i try to show the result on a page called test.php the session seems to be intact and working fine, and i still cant get the trouble of having to paste the code for class declaration on the top of eveypage out. :( VERY confusing.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply