Re: Mixing classes

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

 



Hi!

A good idea, which I have used for some classes, like the Journal class (error handling) which fits in with the Core class. Unfortunantly I have too many classes to make them all into a chain of child classes. And also I have an ambition to write these classes in sorta seperate tools, which I can then use in other projects with no altering of the code.

The database (mysql) class is such a class, which is in use in almost every project I work at.

Would it be possible to create a member variable to a class on the fly, without specifically creating it when you write the class? Something like this:

<?php
class MyClass {
    var $MemberVar1 = "";
    var $MemberVar1 = "";

function MyClass() {
    //constructor
}
function MkMemVar($newmemvar, $memvarval) {
    $this->newmemvar = $memvarval;
    // this is just an example, don't know it would actually be done so
    // but you get the idea: the $newmemvar would be the name of the new
    // member variable and $memvarval would be the value for the new
    // member variable.
}
?>

Or could it even be possible to do this outside of a class? So that I wouldn't have to specify a seperate function for it in my classes?

This way I could make an instance of another class inside any class on the need-bases and it would solve my problem. Is there anyway to accomplish this? I couldn't find any immediately solution by looking at the php manual, but it doesn't mean it's not there.


Davy Obdam wrote:
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.






-- developer & programmer me tomi kaistila home http://www.datamike.org gnupg 0xFA63E4C7

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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