Kirtan's Office

Modular objects?

, , , ,

Have you ever thought about a way of creating a modular class in php? A class, which you can add new methods in for example from the external files?

While I was playing my own CMS, I thought that it would be good if my text-parsing-and-validating class was modular. Why? Well, it is possible, that after some time new page modules will require special text-parsing functions, which I haven't predicted and I don't want to convict anybody (especially me smile ) searching for propriet class.

The problem wasn't easy to solve, but I finally succeeded. Below, I present an example on abstract class decorator and two modules c1 and c2

dekorator.php

class decorator
{
        private $obj;
        public function __call($method, $vars)
        {
                /*
                        Method name consists of classname_methodname()
                        Getting class name.
                */
                $class = explode("_", $method);
                if( isset($this->obj[ $class[0] ]) )
                {
                        return call_user_func_array( array($this->obj[$class[0]], $method), $vars );
                }
        }
        
        public function save($class)
        {
                $this->obj[$class] = new $class();
        }
}

c1.module.php

class c1
{        
        public function __construct()
        {}
        
        public function c1_Method1()
        {
                echo "Hello";
        }
        
        public function c1_Method2($text)
        {
                echo "<h2>$text</h2>";
        }
}

c2.module.php

class c2
{
        public function __construct() {}
        
        public function c2_Method1()
        {
                echo "<pre>Welcome</pre>";
        }
        
        public function c2_Method2($tt, $tt2)
        {
                echo "<h1>$tt</h1><p>$tt2</p>";
        }
}

Modules are implemented the simplest way, but I think, it's easy to imagine this in a more automatic way (for example with the use of __autoload() function):

index.php

include_once("c1.module.php");
include_once("c2.module.php");
$temp  = new c1();
$temp2 = new c2();

$dekorator = new decorator();
$dekorator->save(get_class($temp));
$dekorator->save(get_class($temp2));

Then we can easily use our new methods:

$dekorator->c1_Method1();
$dekorator->c1_Method2("Hello, subclass");

$dekorator->c2_Method1();
$dekorator->c2_Method2("Title", "content");

And Voila. It's more, than possible, that there's an easier way of doing this, but it's only one, I got smile
If you know better way of solving this problem, I'll be glad to hear about it:)

PS.: I'm sorry for my english

Notka sponsorowana #1Akcent świąteczny.

Comments

Unregistered user Tuesday, December 11, 2007 11:06:05 PM

Dabroz writes: Hello, Kirtan! Thank you for adding my sites in your blogroll. :-) About your script: have you ever used Reflection class in PHP? That is sth that you might find useful. Hope that helps! Dab

Maciej KrasuskiKirtan Sunday, January 6, 2008 9:44:26 PM

Thanks Dab.
Yes, Reflections are very helpfull, but I was thinking about sth different.

Marcinsuperlolek Wednesday, January 30, 2008 12:17:43 PM

vell, i think that you should use factory pattern... :] its much easier to maintain smile

Maciej KrasuskiKirtan Wednesday, January 30, 2008 1:20:32 PM

Yup, indeed easier, but, IMO, works almost the same = ]
thx

Marcinsuperlolek Wednesday, January 30, 2008 1:42:35 PM

well maybe, but thx to factory patter you're free how you call the methods.. you doesn't need to remember that this special method is connected with this special class.. and it's a child of it...

in your way of solving that problem probably i would stuck in one week...
i don't say that it's bad, it's good.. very good - imho, nice job, but you should consider, using the easier possible way, and firstly try to find if maybe there is already some design pattern that can solve your problem, before, you'll try to do solve it your own way, trust me, it's much easier.. wink

Maciej KrasuskiKirtan Wednesday, January 30, 2008 4:34:51 PM

but you should consider, using the easier possible way, and firstly try to find if maybe there is already some design pattern that can solve your problem, before, you'll try to do solve it your own way



To be honest, I got to this idea by accident, I haven't been planning this. = ]
Looking to the blog version of my idea, you're right. But, recently, I've changed it a bit. You don't need to know the object/class name, only method. The only bug is that method name must be unique (ex.: you cannot have method meth in more than one class), but for me it's not a problem, and as far as I read about factory pattern, it has the same restriction (or am I wrong?).

Marcinsuperlolek Wednesday, January 30, 2008 4:58:19 PM

well.. sorry, yes you're wrong, with factory pattern all of the objects should have the same method name for best usage... there is only one thing, there should be no duplicated classes, what i mean, is that, you should not have more than two classes in your project with the same name, cause if you use function __autoload() {} you'll get a bit confused, of course, you can get rid with that but it's better to not do that...
maybe i don't understand you or maybe i do... here let me explain it on my blog...
http://my.opera.com/superlolek/blog/2008/01/30/php-factory-pattern

Write a comment

New comments have been disabled for this post.

June 2012
M T W T F S S
May 2012July 2012
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