Frustration calling class from another class.

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
JasonMWaldo
Forum Newbie
Posts: 9
Joined: Wed Jul 26, 2006 2:42 am

Frustration calling class from another class.

Post by JasonMWaldo »

I'm running into a bit of frustration with something that I am trying to do and would appreciate any pointers in the right direction.

I'm setting up a class to do some authentication and a second class to handle the database connections.

The main script calls a method in the first class with an indicator to either open or closed the database connection. The method in the first class, when called to open a connection, will call a new instance of the second class, storing this class indicator in a porperty of the first class. Once this new instance is created it will call a method in the second class to establish the connection. The resource handle for the database connection is held in a property of the second class.

The method in the first class, when called to close the connection, will again call the same method in the second class with an indicator to close the connection.

This all seemed to work fine, until I decided to see if it would work over SSL. The database connection still opens over SSL, but it errors out with Fatal error: Call to a member function dbConnect() on a non-object

I'm wondering if it is how I am calling the method of the second function, and if there is a better way. Here is an abstract of what the calling line looks like:

Code: Select all

$this->InstanceOfSecondClass->DatabaseMethod();
Any help on what is happening would be appreciated.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

post more codes, especially the two classes you are talking about, to let us help you better.
JasonMWaldo
Forum Newbie
Posts: 9
Joined: Wed Jul 26, 2006 2:42 am

Post by JasonMWaldo »

The classes are all very basic at this point, not much done in them, but here is the relevent code, all of the variables coming from the main script are set in an include file. I should also mention that I have only been working with PHP for about 6 months and I had the brilliant idea to try to learn classes on my own.

First the two calling lines from the main script:

Code: Select all

$secureLogin=new authentication();

$secureLogin->connectDatabase($dbServer,$dbUserName,$dbPwd,$database,'open');

$secureLogin->connectDatabase('','','','','close');
The authentication class:

Code: Select all

final class authentication{

  //properties
  public $referer; //page user came from
  public $ipAddress; //user ip address
  private $database; //used for instance of DBAccess class


  //methods

  public function checkReferer(){
   //checks referer against provided value
   return($this->referer==$_SERVER['HTTP_REFERER']);
  }//end checkReferer()

  public function checkIPSingle(){
   //checks user ip address against provided value
   return($this->ipAddress==$_SERVER['REMOTE_ADDR']);
  }//end checkIPSingle()

  public function connectDatabase($dbServer,$dbUserName,$dbPwd,$database,$method){
   //connects and disconnects from MySQL database using provided data, method is
   // used to indicate what to do, acceptable values are 'open' and 'close'
   if ($method=='open'){
    $this->database=new DBAccess();
    $this->database->dbServer=$dbServer;
    $this->database->dbUserName=$dbUserName;
    $this->database->dbPwd=$dbPwd;
    $this->database->database=$database;
    $this->database->dbConnect('open');
   }else if($method=='close'){
    $this->database->dbConnect('close');
   }else{
    print '<h1>CRITICAL ERROR: you must use open or close for method</h1>';
   }//end if
  }//end connectDatabase()

 }//end authentication class
The database access class:

Code: Select all

class DBAccess{

  //properties
  public $dbServer;
  public $dbUserName;
  public $dbPwd;
  public $database;
  public $dbConn;


  //methods

  public function dbConnect($method){
   //used to open and close connections to the database, method can be only
   // 'open' or 'close'
   if ($method=="open"){
    $this->dbConn=mysql_connect($this->dbServer,$this->dbUserName,$this->dbPwd) or die('Could not connect: '.mysql_error());
    mysql_select_db($this->database) or die('Could not select database.');
   }else{
    mysql_close($this->dbConn);
   }//end if
  }//end dbConnect()

 }//end DBAccess class
The DBAccess class is a modification of a function that I have been using for the last two months to handle database access, and it works quite well.

As I said, the above seems to work in a standard http session, but, as soon as I shift it over to an SSL session, the last call from the main script throws an error.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

I would personally prefer code which looks like:

Code: Select all

$db = connectDatabase($dbServer,$dbUserName,$dbPwd,$database);

$secureLogin=new authentication($db);

$db->close();
Your authentication class may need to access the database, but it sure should not be dealing with the responsiblities for setting up and tearing down the connection.

The "open" or "close" method parameters look goofy. This is an object, why not just add another method?

and as for

Code: Select all

$this->database->doSomething();
that is the correct way to do it. If you have many calls to $this->database in the same method, you could just set a local variable, i.e.

Code: Select all

function authMethod() {
  $db = $this->database;
  $db->databaseThing1();
  $db->databaseThing2();
  $db->databaseThing3();
}
JasonMWaldo
Forum Newbie
Posts: 9
Joined: Wed Jul 26, 2006 2:42 am

Post by JasonMWaldo »

Thanks sweatje. That answered quite a few of the concerns. One of the main ones that I had was using the -> twice in the same statement, it looked clunky and I was wondering if it was indeed the correct way to handle this type of call.

I originally used the 'open' and 'close' as part of a regular function, and did so to avoid having to write an additional function. With classes, it would probably be easier just to have a method to open and a method to close the database.

Any idea on why the thing seemed to work with a regular http session, but failed on an SSL session. The server is Apache on Linux BTW.
Post Reply