Jump to content

returning values of an object in a session


rn14

Recommended Posts

I am trying to return the values that are stored in this class at least i think they are:

 

This is my order class:

 


<?
class order{
var $id;
var $id1;
var $id2;
var $id3;
var $id4;
var $id5;
var $id6;

public function _construct
(
$or_id,
$or_id1,
$or_id2,
$or_id3,
$or_id4,
$or_id5,
$or_id6
)
{
$this->id = $or_id;
$this->id1 = $or_id1;
$this->id2 = $or_id2;
$this->id3 = $or_id3;
$this->id4 = $or_id4;
$this->id5 = $or_id5;
$this->id6 = $or_id6;
}

public function getid(){return $id;}
public function getid1(){return $id1;}
public function getid2(){return $id2;}
public function getid3(){return $id3;}
public function getid4(){return $id4;}
public function getid5(){return $id5;}
public function getid6(){return $id6;}

public function setid($id){$this->id = $id;}
public function setid1($id1){$this->id1 = $id1;}
public function setid2($id2){$this->id2 = $id2;}
public function setid3($id3){$this->id3 = $id3;}
public function setid4($id4){$this->id4 = $id4;}
public function setid5($id5){$this->id5 = $id5;}
public function setid6($id6){$this->id6 = $id6;}

}
?>

 

This is where I fill the class with information

 

<?
include('test.php');
session_start();
$meallist = array();
foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } }
print_r($meallist);
echo "<br>";
$result = count($meallist, COUNT_RECURSIVE);
echo $result;
echo "<br>";
$id = $meallist[0][0];
$id1 = $meallist[1][0];
$id2 = $meallist[2][0];
$id3 = $meallist[3][0];
$id4 = $meallist[4][0];
$id5 = $meallist[5][0];
$id6 = $meallist[6][0];

echo "<br>";
echo $id;
echo "<br>";
$class = new order($id,$id1,$id2,$id3,$id4,$id5,$id6);


$_SESSION['class'] = serialize($class);
?>

 

This is where i try and return these values:

 


<?
session_start();
include('test.php');


$class = unserialize($_SESSION['class']);

$class = new order('id','id1','id2','id3','id4','id5','id6');


echo $class ->getid()->id;
echo $class ->getid1()->id1;
?>

 

It just returns nothing and I am certain those values are filled

 

 

Thanks in advance for any help

Link to comment
Share on other sites

and this:

public function getid(){return $id;}
public function getid1(){return $id1;}
public function getid2(){return $id2;}
public function getid3(){return $id3;}
public function getid4(){return $id4;}
public function getid5(){return $id5;}
public function getid6(){return $id6;}

 

needs to be this:

public function getid(){return $this->id;}
public function getid1(){return $this->id1;}
public function getid2(){return $this->id2;}
public function getid3(){return $this->id3;}
public function getid4(){return $this->id4;}
public function getid5(){return $this->id5;}
public function getid6(){return $this->id6;}

Link to comment
Share on other sites

No sure on this, but would you not just call the get id like so:

 

<?php
session_start();
include('test.php');


$class = unserialize($_SESSION['class']);
// $class = new order('id','id1','id2','id3','id4','id5','id6');  Commented out do to overwriting.
echo $class->getid();
echo $class->getid1();
?>

 

I am not 100% sure, but I think that is how you should access them? Also are you not overwriting class with the line above the echos?

 

Editied thanks to flyhoney for correcting.

Link to comment
Share on other sites

No sure on this, but would you not just call the get id like so:

 

<?php
session_start();
include('test.php');


$class = unserialize($_SESSION['class']);
// $class = new order('id','id1','id2','id3','id4','id5','id6');  Commented out do to overwriting.
echo $class->getid;
echo $class->getid1;
?>

 

I am not 100% sure, but I think that is how you should access them? Also are you not overwriting class with the line above the echos?

 

That is how you access class variables, not class functions.

 

class Foo {
    $var = 'test';

    function bar () {
        echo 'baz';
    }
}

$foo = new Foo();
echo $foo->var; // prints 'test'
$foo->bar(); // prints 'baz'

Link to comment
Share on other sites

No sure on this, but would you not just call the get id like so:

 

<?php
session_start();
include('test.php');


$class = unserialize($_SESSION['class']);
// $class = new order('id','id1','id2','id3','id4','id5','id6');  Commented out do to overwriting.
echo $class->getid;
echo $class->getid1;
?>

 

I am not 100% sure, but I think that is how you should access them? Also are you not overwriting class with the line above the echos?

 

That is how you access class variables, not class functions.

 

class Foo {
    $var = 'test';

    function bar () {
        echo 'baz';
    }
}

$foo = new Foo();
echo $foo->var; // prints 'test'
$foo->bar(); // prints 'baz'

 

Yea, I meant to keep the parans there and just remove the ->id;

 

My mistake on that part, thanks for correcting.

Link to comment
Share on other sites

Check out the modifications I made:

 

 

I am trying to return the values that are stored in this class at least i think they are:

 

This is my order class:

 

<?php
class order{
    var $id;
    var $id1;
    var $id2;
    var $id3;
    var $id4;
    var $id5;
    var $id6;

    public function _construct($or_id, $or_id1, $or_id2, $or_id3, $or_id4, $or_id5, $or_id6) {
        $this->id = $or_id;
        $this->id1 = $or_id1;
        $this->id2 = $or_id2;
        $this->id3 = $or_id3;
        $this->id4 = $or_id4;
        $this->id5 = $or_id5;
        $this->id6 = $or_id6;
    }

    public function getid(){return $this->id;}
    public function getid1(){return $this->id1;}
    public function getid2(){return $this->id2;}
    public function getid3(){return $this->id3;}
    public function getid4(){return $this->id4;}
    public function getid5(){return $this->id5;}
    public function getid6(){return $this->id6;}

    public function setid($id){$this->id = $id;}
    public function setid1($id1){$this->id1 = $id1;}
    public function setid2($id2){$this->id2 = $id2;}
    public function setid3($id3){$this->id3 = $id3;}
    public function setid4($id4){$this->id4 = $id4;}
    public function setid5($id5){$this->id5 = $id5;}
    public function setid6($id6){$this->id6 = $id6;}

}
?>

 

This is where I fill the class with information

 

<?
include('test.php');
session_start();
$meallist = array();
foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } }
print_r($meallist);
echo "<br>";
$result = count($meallist, COUNT_RECURSIVE);
echo $result;
echo "<br>";
$id = $meallist[0][0];
$id1 = $meallist[1][0];
$id2 = $meallist[2][0];
$id3 = $meallist[3][0];
$id4 = $meallist[4][0];
$id5 = $meallist[5][0];
$id6 = $meallist[6][0];

echo "<br>";
echo $id;
echo "<br>";
$class = new order($id,$id1,$id2,$id3,$id4,$id5,$id6);


$_SESSION['class'] = serialize($class);
?>

 

This is where i try and return these values:

 


<?
session_start();
include('test.php');


$class = unserialize($_SESSION['class']);

$class = new order('id','id1','id2','id3','id4','id5','id6');


echo $class->getid();
echo $class->getid1();
?>

 

It just returns nothing and I am certain those values are filled

 

 

Thanks in advance for any help

Link to comment
Share on other sites

Your overiding $class with another instance.

 

<?
session_start();
include('test.php');


$class = unserialize($_SESSION['class']);

$class = new order('id','id1','id2','id3','id4','id5','id6');


echo $class->getid();
echo $class->getid1();
?>

 

Should be....

 

<?php
session_start();
include('test.php');


$class = unserialize($_SESSION['class']);


echo $class->getid();
echo $class->getid1();
?>

 

ps: This is a particularly poorly designed piece of code. What happens if you need to add more items than the class can currently handle? You'd need to modify the class.

Link to comment
Share on other sites

I have taken out that piece that overrides and still cant get it to work. I am fairly new to this so apologies for the bad code. I would like to know how to form this better and for it to be able to handle more items. if you could even point me in the right direction here that would be great?

Link to comment
Share on other sites

I would break it up into two classes:

 

<?php
class Order {

private $items = array();

function __construct() {

}

function addItem($item) {
	$this->items[] = $item;
}

function getItems() {
	return $this->items;
}

}

class Item {

private $id;

function __construct($id) {
	$this-id = $id;
}

function setId($id) {
	$this->id = $id;
}

function getId() {
	return $this->id;
}

}
?>

 

And you would use it like this:

 

<?php
$order = new Order();
$item = new Item(100);
$order->addItem($item);

$items = $order->getItems();
foreach ($items as $item) {
   echo $item->getId();
}
?>

Link to comment
Share on other sites

Im really trying to understand this but would really appreciate a little bit more help. I need to return each of those id's for every item would it be neccessary to have an array for id's aswell? Will I still use a session just as I have??

Link to comment
Share on other sites

I'm sorry, I think I may have tried to throw too much at you.  You probably want to read more on classes to get a better feel for exactly how to use them.

 

You're original code, with the modifications we mentioned, will work, it just may not be optimal.  If I were you, I would start using a lot of echo statements, and see what happening at every point in the script, so you can narrow things down to which line of code is giving you trouble.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.