Jump to content

PHP Classes Troubles


d_barszczak

Recommended Posts

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]
<?php

class 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
Share on other sites

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]<?php
class session extends mysqldb {
?>[/code]
All public methods and properties of mysqldb will come available to session:
[code]<?php
parent::connect();
?>[/code]
or
[code]<?php
$this->connect();
?>[/code]

2) Have a property of session contain an instantiation of mysqldb:
[code]<?php
class 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
Share on other sites

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
Share on other sites

[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
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.