我正在使用CMS,Joomla,並且有一個核心類將一組參數呈現給表單JParameter。基本上它有一個render()
函數,它輸出一些與表格相關的HTML,這與我的其他站點不一致。在運行時更改對象的類
對於可維護性的問題,因爲我不知道在哪裏使用它,所以我不想更改核心代碼。最理想的是能夠定義一個擴展JParameter的新類,然後將我的$ params對象放到這個新的子類中。
// existing code --------------------
class JParameter {
function render() {
// return HTML with tables
}
// of course, there's a lot more functions here
}
// my magical class -----------------
class MyParameter extends JParameter {
function render() {
// return HTML which doesn't suck
}
}
// my code --------------------------
$this->params->render(); // returns tables
$this->params = (MyParameter) $this->params; // miracle occurs here?
$this->params->render(); // returns nice html
我不認爲這是可能的一個很好的方式。 (好吧,這是可能的,但我看到它的做法是涉及到serialize()和str_replace()的破解。可能不是你想要的可維護性是這裏的問題......) – flussence 2008-12-23 01:19:55
serialize和str_replace?哎呀! – nickf 2008-12-23 01:34:22