Modular objects?
Sunday, November 25, 2007 11:46:56 PM
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







Unregistered user # Tuesday, December 11, 2007 11:06:05 PM
Maciej KrasuskiKirtan # Sunday, January 6, 2008 9:44:26 PM
Yes, Reflections are very helpfull, but I was thinking about sth different.
Marcinsuperlolek # Wednesday, January 30, 2008 12:17:43 PM
Maciej KrasuskiKirtan # Wednesday, January 30, 2008 1:20:32 PM
thx
Marcinsuperlolek # Wednesday, January 30, 2008 1:42:35 PM
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..
Maciej KrasuskiKirtan # Wednesday, January 30, 2008 4:34:51 PM
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
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