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

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
Black Unicorn
Forum Commoner
Posts: 48
Joined: Mon Jun 16, 2003 9:19 am
Location: United Kingdom

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

Post 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]
Last edited by Black Unicorn on Wed Sep 15, 2004 8:21 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is $_SESSION["basket"] the same instance of the class? if so, use $this.
Black Unicorn
Forum Commoner
Posts: 48
Joined: Mon Jun 16, 2003 9:19 am
Location: United Kingdom

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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";
   }
}
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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]
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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
Black Unicorn
Forum Commoner
Posts: 48
Joined: Mon Jun 16, 2003 9:19 am
Location: United Kingdom

Post by Black Unicorn »

Thanks all for your help. I'll chase up on your suggestions.
Best whishes,
H
Post Reply