d_barszczak Posted July 26, 2006 Share Posted July 26, 2006 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.[i]Fatal error: Call to a member function connect() on a non-object in D:\www\LocalUser\callmanager\classes\sessions.conf.php on line 12[/i]Example code would be as follows.[code]<?phpclass sessions { function check() { $sesid = session_id(); $query = "SELECT * FROM sessions WHERE id = '$sesid'"; $mysqldb->connect();}}[/code]You can call $mysqldb->connect(); from anywhere except from withing the class tags. Link to comment https://forums.phpfreaks.com/topic/15668-php-classes-troubles/ Share on other sites More sharing options...
d_barszczak Posted July 26, 2006 Author Share Posted July 26, 2006 Its ok sorted it now! Link to comment https://forums.phpfreaks.com/topic/15668-php-classes-troubles/#findComment-63889 Share on other sites More sharing options...
448191 Posted July 26, 2006 Share Posted July 26, 2006 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:[code]<?phpclass session extends mysqldb {?>[/code]All public methods and properties of mysqldb will come available to session:[code]<?phpparent::connect();?>[/code]or[code]<?php$this->connect();?>[/code]2) Have a property of session contain an instantiation of mysqldb:[code]<?phpclass session {private mysqldb;function __construct(){ $this->mysqldb = new mysqldb();}?>[/code]Latter option example only works with php 5, but same can be accomplished with php4. Link to comment https://forums.phpfreaks.com/topic/15668-php-classes-troubles/#findComment-63895 Share on other sites More sharing options...
d_barszczak Posted July 26, 2006 Author Share Posted July 26, 2006 Not the way i did it but your ways better so thankyou for your comments. Link to comment https://forums.phpfreaks.com/topic/15668-php-classes-troubles/#findComment-63922 Share on other sites More sharing options...
trq Posted July 26, 2006 Share Posted July 26, 2006 I wouldn't recomend making the [i]session[/i] class an extension of the [i]mysql[/i] class... they have nothing to do with each other. Thats just plain bad OOP.The later approuch is a more appropriate solution. Link to comment https://forums.phpfreaks.com/topic/15668-php-classes-troubles/#findComment-64025 Share on other sites More sharing options...
448191 Posted July 26, 2006 Share Posted July 26, 2006 [quote author=thorpe link=topic=101896.msg403854#msg403854 date=1153922888]I wouldn't recomend making the [i]session[/i] class an extension of the [i]mysql[/i] class... they have nothing to do with each other. Thats just plain bad OOP.The later approuch is a more appropriate solution.[/quote]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! :) Link to comment https://forums.phpfreaks.com/topic/15668-php-classes-troubles/#findComment-64081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.