Current location: Hot Scripts Forums » Programming Languages » PHP » problem with classes


problem with classes

Reply
  #1 (permalink)  
Old 07-04-05, 01:48 PM
filth filth is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
problem with classes

Hi All

I have only just started using inheritance etc and am lost a bit.

I am using the DB pear package but I am writing a class that will hopefully use the pear package.

My class uses extends DB after the name of it.

When I call the connect function that is within the DB class it works with no problem but when I try and use the query function I get the function does not exist error message. Having looked in the DB pear package files I have found that the connect function is within the main DB class in DB.php but the query function is in the common.php file.

How do I go about it so that my class can use every function within the DB class.
Reply With Quote
  #2 (permalink)  
Old 07-04-05, 03:16 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

Most probably you ate not useing this or parent keywords before the method names. For example, you should call $this->query() to call this class's query() method and call parent::query() to call the method that originally was defined in the parent class and overridden in your class.

To better understand your problem, please post the code here.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #3 (permalink)  
Old 07-04-05, 03:44 PM
filth filth is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
hi thank you for your response, I did make that mistake when I first wrote the code but subsequently fixed it.

The relevant code is as follows:-

PHP Code:

class my_class extends DB


function 
check_user($user_name)
 {
  global 
$_SERVER;
  
$sql 'select * from users where username = \''.htmlspecialchars($user_name).'\' AND browser = md5(\''.mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']).'\') AND session_id = md5(\''.htmlspecialchars(session_id()).'\')';
  
$confirm_user parent::query($sql);
  if (
$confirm_user->numRows()==1)
  {
   
$_SESSION['logged_in'] = true;
  }
 }

as you can see I have tried

PHP Code:

parent::query($sql); 

I have also tried

PHP Code:

DB::query($sql); 

but that does not work either, in both cases it displays an error saying the following:-
PHP Code:

 Fatal errorCall to undefined function: query() in /home/petermcd/public_html/the.php on line 25 

The connect function is in the main class which works fine (code for that not shown), the query function is called from a class in common.php which has a class in mysql.php that it is being extended from (that is the database type being used in this case). The mysql.php containing class is included when the class finds out what database type is being used.

Last edited by filth; 07-04-05 at 04:31 PM.
Reply With Quote
  #4 (permalink)  
Old 07-04-05, 04:29 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
There is no point for you to extend the DB class, or any of the drivers. You just will call them from your client code. There would be a point to extend the DB_common class if you were writing another driver, but you are not. Simply use the Pear drivers, but do not extend them.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #5 (permalink)  
Old 07-04-05, 04:39 PM
filth filth is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
ok cheers, so looks like I was just making life difficult for myself

I have just modified it so it can use the DB class without extending it and worked first time, begers the question why didn't I do that to start with lol.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A Language filter problem, and a problem with a for loop querying my database. . . Spreegem PHP 6 05-08-05 11:03 AM
Looping problem mcrob PHP 2 04-13-05 10:21 AM
Count problem kasic ASP.NET 1 10-20-04 12:23 AM
Asp and Microsoft Access 2002 problem gop373 ASP 2 10-06-04 09:13 AM
an unusual problem.... fabulosas10 PHP 0 07-16-04 02:03 AM


All times are GMT -5. The time now is 06:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.