Re: Mixing classes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi

Maybe you could make most class childs of the Core class... well at least the classes that need values from you config file.

class Database extends Core
{
}


}

I dont know if this is gonna work in your set up, but since this Core class reads the config file, now you can just creat an object from the database class anywhere and alread have those variables available.

Best regards,

Davy

Tomi Kaistila wrote:

Hi!

Thanks for the idea, hadn't actually thought of it that way. I had thought of passing objects (instance the db object to the auth object) by reference, but when thinking that while one class might only need the help of one other class, another class might need the help of four classes. Would make the constructor look a bit ugly.

The approuch you suggest is good, for most of the classes. For example, although I create an instance of the db class on the web site, it doesn't harm to create another inside the auth class. But there are some classes that are not suited to be created that way. For example, the Core class, which for one thing, reads the configuration file from the include path and sets a load of values which each class should then use. For example, the db class needs values such as the username, password, hostname, port, etc. It wouldn't be advisable read the configuration file more than once.

I'm not sure, but could an instance of a class be pass as reference with the Global keyword? Like so;

<?php
class auth {
    var $dbObject = "";

function auth () {
    //Create an instance of you database class
    global &$dbobject;
    $this->dbObject = $dbobject;
}

function doSomething() {
    // Execute a query
    $this->dbObject->query($sql);

}
}
?>

Davy Obdam wrote:
>

Hi Tomi,

You could create an object of the Database class in your Auth class like this and then use your Datebase class with your Auth class:

<?php
require_once("Database/Database.php");

class Auth
{

var $dbObject = "";

function Auth ()
{
   //Create an instance of you database class
   $this->dbObject = new Database($arg1, $arg2, $etc);
}

function doSomething()
{
   // Execute a query
   $this->dbObject->query($sql);

  }

}

?>

I hope this helps

Best regards,

Davy Obdam

Tomi Kaistila wrote:

Hey!

I've written several general classes, much as tools. One manages databases, one authenticates users, etc. I'm trying to bring all of these together into one sorta like a CMS, but the problem is that I can't find a way to have the different classes take advantage of each other; for example, a function in the auth class needs to contact the database, so it needs the query() function from the database class, but the objects are created on the index.php and as such are not in the scope of the functions in the classes.

Would anyone have any ideas or suggestions how to solve this issue? I've looked for a solution for some time now, to no result.

My PHP version is 4.3.8.





-- Davy Obdam mailto:info@xxxxxxxxxxxxx web: http://www.davyobdam.com

The media finally figured out that their "paying customers" (i.e. advertisers) don't WANT an intelligent, thoughtful audience. And they no longer have one." (Rich Tietjens)

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux