Jump to content


Photo

PHP Classes Troubles


  • Please log in to reply
5 replies to this topic

#1 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 26 July 2006 - 09:36 AM

Hi,

Ok basically i have 2 classes on 2 seperate php files one controls the database functions for my app and the other controls the session management so that a list of current online users can be displayed.

The problem im having is that the sessions class needs to use some functions from the database class but all i get is this error code.


Fatal error: Call to a member function connect() on a non-object in D:\www\LocalUser\callmanager\classes\sessions.conf.php on line 12

Example code would be as follows.

<?php

class sessions {
      
function check() {
 	$sesid = session_id();
 	$query = "SELECT * FROM sessions WHERE id = '$sesid'";
	
	$mysqldb->connect();
}

}

You can call $mysqldb->connect(); from anywhere except from withing the class tags.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#2 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 26 July 2006 - 09:48 AM

Its ok sorted it now!

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 26 July 2006 - 09:58 AM

Argh. You already posted you sorted it out while I was writing this, but I'll post it anyway.

You need to read up on 'variable scope'.

For now, to fix this, you have 2+ options:

1) have session EXTEND mysqldb:
<?php
class session extends mysqldb {
?>
All public methods and properties of mysqldb will come available to session:
<?php
parent::connect();
?>
or
<?php
$this->connect();
?>

2) Have a property of session contain an instantiation of mysqldb:
<?php
class session {
private mysqldb;

function __construct()
{
  $this->mysqldb = new mysqldb();

}
?>

Latter option example only works with php 5, but same can be accomplished with php4.

#4 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 26 July 2006 - 10:43 AM

Not the way i did it but your ways better so thankyou for your comments.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 July 2006 - 02:08 PM

I wouldn't recomend making the session class an extension of the mysql class... they have nothing to do with each other. Thats just plain bad OOP.

The later approuch is a more appropriate solution.

#6 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 26 July 2006 - 03:04 PM

I wouldn't recomend making the session class an extension of the mysql class... they have nothing to do with each other. Thats just plain bad OOP.

The later approuch is a more appropriate solution.


Just out of curiousity; when would you extend one class with another? If need practicly all of the parents methods, how can a claim they aren't related hold any ground?

I'm asking because I've got a LOT of classes that need each others methods and props. Some of them are in this long chain of extends, some are referered to by object references. I have it depend on wether you need the parent class in the child class on most logicpaths...

Some thoughts one this are more than welcome!  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users