Page 1 of 1

[SOLVED] Can't get class to call its method

Posted: Tue Sep 14, 2004 4:59 am
by Black Unicorn
I have a problem. I am including a database lookup in my class but can't get the query method to work when called from the class itself.

This object is stored in a session, and called using $_SESSION["basket"]->method

The query method works fine when called from without the class, e.g. $_SESSION["basket"]->Connect (blah, blah,blah,blah)
and
$_SESSION["basket"]->Query(query) as well.
Any reason why it fails when the addItem method fails?
Help greatly appreciated.

Code: Select all

<?
class Basket{
	var $TEMP;
	var $db_host;
	var $db_conn;
	var $db_user;
	var $db_pass;
	var $db_base;
	var $items;
	var $itemInfo;
	function Connect($host,$user,$pass,$base){
		$this->db_host = $host;
		$this->db_user = $user;
		$this->db_pass = $pass;
		$this->db_base = $base;
		$this->db_conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
		if (!$this->db_conn)die ("Fatal error. Could not connect!");
		mysql_select_db($this->db_base,$this->db_conn) or die ("Could not select ".$this->db_base."!!!");
	}
	function Query($q){
		$tmp = "";
		$info_elements = "";
		$r = @mysql_query($q,$this->db_conn);
		if (!$r)return false;
		while($tmp = mysql_fetch_array($r)){
			$info_elements[]=$tmp;
		}
		return $info_elements;
	}

	function addItem($id){
		if (isset($this->items[$id])){
			$this->items[$id]++;
			$q = "select * from item where id=$id";
			$this->itemInfo[$id] = $_SESSION["basket"]->Query($q);
			print_r($this->itemInfo);
		}
		else $this->items[$id] = 1;
	}
}
?>
?>[/php_man]

Posted: Tue Sep 14, 2004 5:09 am
by feyd
is $_SESSION["basket"] the same instance of the class? if so, use $this.

Posted: Tue Sep 14, 2004 6:02 am
by Black Unicorn
That worked a charm, thanx a lot.
I am daring OOP for the first time in my career, and still need to get my head around it.

Only one more thing, does PhP benefit from constructor functions? Or is this only PhP 5.
Best regards,
H

Posted: Tue Sep 14, 2004 9:29 am
by kettle_drum
In pre php5 you can make a constructor by naming a method the same as the class.

Code: Select all

class rahh {
   function rahh(){
      echo  "class constructed";
   }
}

Posted: Tue Sep 14, 2004 9:32 am
by CoderGoblin
I always found the constructor a useful place to initialise variables or set them to meaningful defaults. Set variable to "" if it was to be a string, 0 if integer etc. Made the "type" more obvious if someone else was to modify the code later.

Posted: Tue Sep 14, 2004 10:07 am
by m3mn0n
Constructors are great if you want to set an "active state" for the class member variables to be in at the time of execution.

If you don't need to have a predefined active state, then you don't really need a constructor (or so I've read).

[big_search]php classes constructors[/big_search]

Posted: Tue Sep 14, 2004 3:36 pm
by McGruff
Black Unicorn wrote:I am daring OOP for the first time in my career, and still need to get my head around it.
Check out http://www.phppatterns.com.

I'd strongly encourage you to start unit testing as soon as possible - see link in my signature.

Also: http://www.developerspot.com/tutorials/ ... page1.html

Posted: Wed Sep 15, 2004 8:19 am
by Black Unicorn
Thanks all for your help. I'll chase up on your suggestions.
Best whishes,
H