Skip navigation.

exploreopera

| Help

Sign up | Help

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

avatar
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

By anonymous user, # 11. December 2007, 23:06:05

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

By Kirtan, # 6. January 2008, 21:44:26

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

By superlolek, # 30. January 2008, 12:17:43

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

By Kirtan, # 30. January 2008, 13:20:32

avatar
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:

By superlolek, # 30. January 2008, 13:42:35

avatar
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?).

By Kirtan, # 30. January 2008, 16:34:51

avatar
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

By superlolek, # 30. January 2008, 16:58:19

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

Please type this security code : 94ac04

Smilies