Jump to content

Class problems (crazy variable)


XiaoRulez

Recommended Posts

This is a class i wrote and it's giving me crap -_- it should be counting up but the thing is, it's just staying at 0 and not changing, or any changes made goes back to 0 again ($items variable)... (think it's got to do with php thinking that $itams = 0, the 0 is seen to be NULL?)

can anyone spot any mistakes?

[code]class lists {
private $item = array();
private $items;

function lists() {
$items = 0;
}

function push_back($var) {
$item[$items] = $var;
$items++;
echo '[New Item] Current Items: '.$items.'<br>'."\n";
}

function push_front($var) {
$temp = array();
$temp[0] = $null;
$count = 0;
foreach($item as $i_one) {
$count++;
$temp[$count] = $i_one;
}
$item = $temp;
$items++;
}

function pop_back() {
$temp = $item[$items-1];
$item[$items-1] == $null;
$items--;
return $temp;
}

function pop_front() {
$temp = $item[0];
$temp2;
$count = 0;
foreach($item as $i_one) {
$count++;
if ($count != 1) $temp2[$count] = $i_one;
}
$temp2[$count] = $null;
$item = $temp2;
$items--;
return $temp;
}

function size_of() {
return $items;
}

function erase($loc) {
if ($loc > $items || $loc < 0) return $false;
else {
$temp = $item[0];
$temp2;
$count = $loc;
foreach($item as $i_one) {
$count++;
if ($count != 1) $temp2[$count] = $i_one;
}
$temp2[$count] = $null;
$item = $temp2;
$items--;
return $true;
}
}

function at($loc) {
return $item[$loc];
}
}

$a = &new lists();

function new_a($name) {
global $a;
$size = $a->size_of();
$a->push_back($name);
}
$name2 = "who";
new_a($name2);
new_a($name2);
new_a($name2);[/code]

[quote][New Item] Current Items: 1
[New Item] Current Items: 1
[New Item] Current Items: 1[/quote]
Link to comment
Share on other sites

The problem is you are not using the variables you have defined in your class. In order to refer to the class variables you'll need to use $this->varname in order access the variables within the class. Otherwise the variables you use within a function will not eference variables outside of the function.

So when you use $item or $items you'll want to use $this->item and $this->items with in your methods (the functions).

In order to understand this behaviour you might want to read up on variable-scope
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.