Professional Web Applications Themes

Problem making a class extending another - PHP Development

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

  1. #1

    Default Problem making a class extending another

    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.


    Peter Guest

  2. #2

    Default Re: Problem making a class extending another

    Peter wrote: 
    To get further help you should present some code you are using. And I
    don't mean the code of the pear class. I mean just the code you use to
    use the pear class.

    Regards
    Stefan
    Stefan Guest

  3. #3

    Default Re: Problem making a class extending another

    > To get further help you should present some code you are using. And I 

    Hi

    I have taken out any unnecessary 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 parent::query($sql); I have also tried
    DB::query($sql); but that does not work either, in both cases it displays an
    error saying the following:-

    Fatal error: Call to undefined function: db() in
    /home/petermcd/public_html/the.php on line 25


    Peter Guest

  4. #4

    Default Re: Problem making a class extending another

    Peter wrote: 
    >
    >
    > Hi
    >
    > I have taken out any unnecessary 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 parent::query($sql); I have also tried
    > DB::query($sql); but that does not work either, in both cases it displays an
    > error saying the following:-
    >
    > Fatal error: Call to undefined function: db() in
    > /home/petermcd/public_html/the.php on line 25
    >
    >[/ref]
    I'm not very familar with the pear packages. As I can see is query a
    method of a class called DB. And since you derived your class of DB you
    should simply call query() without anything before (e.g. parent or DB).

    Another thing that makes me stuck is that the error message tells you
    something about a function called db() and I don't see such a function
    in your code?!

    Regards
    Stefan
    Stefan Guest

  5. #5

    Default Re: Problem making a class extending another

    > I'm not very familar with the pear packages. As I can see is query a 

    Sorry the error message should have been

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

    I have tried just calling it by the function name and this has the same
    result, I am presuming it is the way that it calls this function.


    Peter Guest

  6. #6

    Default Re: Problem making a class extending another

    Peter wrote: 
    >
    >
    > Sorry the error message should have been
    >
    > Fatal error: Call to undefined function: query() in
    > /home/petermcd/public_html/the.php on line 25
    >
    > I have tried just calling it by the function name and this has the same
    > result, I am presuming it is the way that it calls this function.
    >
    >[/ref]
    Ok after reading I found out that the query function is not a member of
    the DB class.

    Its member of the connection class. Use like this:

    $conn = DB::connect ($dsn, $options);
    $result = $conn->query ($query_str);


    More information at:
    http://www.kitebird.com/articles/peardb.html

    May you ask aunt google from time to time ;)

    Regards
    Stefan
    Stefan Guest

  7. #7

    Default Re: Problem making a class extending another

    > Ok after reading I found out that the query function is not a member of 

    Thanks Stefan much apreciated, I did have a search on google but couldn't
    find anything, obviously looking for the wrong thing :>


    Peter Guest

Similar Threads

  1. Replies: 2
    Last Post: December 25th, 02:35 PM
  2. Replies: 0
    Last Post: December 6th, 04:34 PM
  3. #38943 [NEW]: serious misbehaviour extending ZipArchive class
    By judas dot iscariote at gmail dot com in forum PHP Bugs
    Replies: 1
    Last Post: September 24th, 10:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139