LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-16-2006, 02:49 AM   #1
unreal128
Member
 
Registered: Jun 2003
Distribution: SuSE, Slackware, Gentoo
Posts: 207

Rep: Reputation: 30
Unhappy PHP: Calling class functions from another class.


I am going crazy trying to figure this out. I want to be able to call object functions from one class to another class. So for example, I have a classDB where I can connect to my database and a classAuth that handles cookies. I would like to be able to call some classDB functions from a classAuth object without having to extend it. If I extended it, then I would just be creating another uneeded database connection. Here is a coded example of what I am talking about.

Code:
class classDB
{
    var $connection;
    ...

    connect()
    {
        $this->connection = mysql_connect(blahblah);
        mysql_select_db($this->connection, $dbname);
    }
    query($sqlquery) 
    {
        if (mysql_query($this->connection, sqlquery)) {
            return true;
        } else {
            return false;
        }
    }
}

class classAuth
{
    checkCookie($obj)
    {
        $cookie_user = read_cookie_data(blah);
        $cookie_password = read_cookie_data(blah);
!!! --> if ( $cookie_password == $obj->query($connection, 'SELECT password FROM user WHERE ...')) {
            return true;
        } else {
            return false;
        }
    }
}

$db = new classDB;
$auth = new classAuth;

$db->connect();
$auth->checkCookie($db);
Can I do something like this. I have checked out all the PHP class documentation but can't find any decent info on this. Please help.
 
Old 11-16-2006, 09:15 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,159

Rep: Reputation: 258Reputation: 258Reputation: 258
Hi

That code seems fine if you ask me.

However, if you changed the query function to something like this:

Code:
    query($sqlquery) 
    {
        if (mysql_query(sqlquery)) {
            return true;
        } else {
            return false;
        }
    }
Notice I changed it so it doesn't use any members, since MySQL doesn't really need it.

You can then just write e.g.
Code:
classDB::query("INSERT INTO...");
since the query function doesn't use any members.

There is however, something wrong with the query function. It returns true or false - the result from a SELECT query is simply lost, so it will never return a password.

Last edited by Guttorm; 11-16-2006 at 09:17 AM.
 
Old 11-17-2006, 12:24 AM   #3
unreal128
Member
 
Registered: Jun 2003
Distribution: SuSE, Slackware, Gentoo
Posts: 207

Original Poster
Rep: Reputation: 30
You are definently right, the query class function doesn't catch results. I just wrote the code on the fly as an example of my point. My concern is that if I make class function calls within another class, I want to know how to pass the object. So for example, the query function is based on the establishment of a database $connection resource. I need to be able to pass that connection resource into the classAuth functions when they are called.
 
Old 11-17-2006, 06:04 AM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
As $db is a global variable, why not use 'global' statement as :
PHP Code:
...
checkCookie()
    {
        global 
$db;
        
$cookie_user read_cookie_data(blah);
        
$cookie_password read_cookie_data(blah);
!!! --> if ( 
$cookie_password == $db->query($connection'SELECT password FROM user WHERE ...')) {
            return 
true;
        } else {
            return 
false;
        }
    }
...
$auth->checkCookie(); 
 
Old 11-22-2006, 01:34 AM   #5
unreal128
Member
 
Registered: Jun 2003
Distribution: SuSE, Slackware, Gentoo
Posts: 207

Original Poster
Rep: Reputation: 30
Good suggestion but I like to directly pass variables/objects specifically. What I ended up doing is passing the other class object to the other class upon class creation.

So for example, in my main page...
PHP Code:
require_once("lib/classCommon.php5");
require_once(
"lib/classDB.php5");
require_once(
"lib/classAuth.php5");
require_once(
"lib/classMarkup.php5");

$util   = new classCommon;
$msg    = new classCommon;
$db     = new classDB(&$util, &$msg);
$auth   = new classAuth(&$util, &$msg, &$db);
$markup = new classMarkup(&$msg, &$db, &$auth); 
Then a snippet of one of the classes (classMarkup.php5) using the object.
PHP Code:
function windowFormLogin()
{
    
$html_content .= '<div class="windowHeader2"> :: Login :: </div>'."\r"
        
.'<div class="windowFrame2">'."\r";
    
$html_content .= $this->msg_object->msgs['auth'];
    
$html_content .= $this->formLogin();
    
$html_content .= '</div>'."\r";

    return 
$html_content;

Do you guys agree?
 
Old 11-24-2006, 04:04 AM   #6
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
To prevent creation of unneeded database connections, it is popular to embed the database connection in a "Singleton" design pattern.

For example, see: Five common PHP design patterns

Freddy Vulto
http://www.fvue.nl/wiki
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 09:05 AM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-22-2006 12:09 AM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 11:14 AM
PHP: how to use a method from a class in the same class ldp Programming 5 09-17-2004 10:52 AM
Need help with C++ class functions :( starla827 Programming 4 03-26-2004 08:43 PM


All times are GMT -5. The time now is 06:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration