Modular objects?
Sunday, 25. November 2007, 23:46:56
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
) 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 ![]()
If you know better way of solving this problem, I'll be glad to hear
about it:)
PS.: I'm sorry for my english
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
Yes, Reflections are very helpfull, but I was thinking about sth different.
By Kirtan, # 6. January 2008, 21:44:26
By superlolek, # 30. January 2008, 12:17:43
thx
By Kirtan, # 30. January 2008, 13:20:32
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..
By superlolek, # 30. January 2008, 13:42:35
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
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