Monday, May 15, 2006

C++ like template in PHP for duplicatable

GIJOE suggests utilizing XOOPS_TRUST_PATH[1] for polymorphism in his article. That's great. The pair of Nuke type and PageController has some weak points for 'duplicatable'. It's difficult to write excellent code about it. He tries to work out such problems.

I saw a strange code that realizes the same mechanism in the intra-net web program of my company before. I can't write the detail of it. But, it's like the following:

define_class('myData', 'tableA');
$instance = new myDataHandler();

function
define_class($className, $tableName)
{
//
// Define class
//
$code = "class {T}_Handler {" ...

$code = str_replace("{T}", $className);
$code = str_replace("{table}", $tableName);

eval(
$code);
}


It's simple C++ template mechanism by eval(). It's just about secure because the executing code is written directly into function. I got a hint from it and developed ClassTemplate of extools[2].

eval() is used to 'dupulicatable' mechanism in XOOPS. The template mechanism like C++ is the unity procedure for use of eval(). It may be possible to take the design easy. But, eval() often makes a security hole. When you'll use it sometime, be careful.

---
[1] XOOPS_TRUST_PATH is the secure directory that web browser can't access to.
[2] extools is the code generator for exFrame (XOOPS class library).

No comments: