On Wed, 20 Oct 2004 08:09:53 +0300, Tomi Kaistila <[EMAIL PROTECTED]> wrote:

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.



http://www.phppatterns.com/index.php/article/articleview/6/1/1/

The approach I generally take is to have a factory class store the singleton instance of each class which may need to be called by others, and create a reference to each needed class in the constructor. E.g.

class DBase {
        function getUser($UserID) { return new User; }
}
class Output {
        function displayStr($str) { echo $str; }
}
class Factory {
        function &getDbase() {
                static $instance;
                if(!isset($instance))
                        $instance =& new DBase;
                return $instance;
        }
        function &getOutput() {
                static $instance;
                if(!isset($instance))
                        $instance =& new Output;
                return $instance;
        }
}
class DoStuff {
        private $db;
        private $out;
        function __constructor() {
                $db =& Factory::getDbase();
                $out =& Factory::getOutput();
        }
        function doStuff() {
                $out->displayStr($db->getUser(0)->Name);
        }
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

http://www.smempire.org

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



Reply via email to