A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearChange the class of an object at runtime - page 1
User InfoPosts
Change the class of an object at runtime#1
I'm working with a CMS, Joomla, and there's a core class which renders a set of parameters to a form, JParameter. Basically it has a render() function which outputs some table-laden HTML which is not consistent with the rest of my site.

For issues of maintainability, and because I have no idea where else this is being used, I don't want to change the core code. What would be ideal would to be able to define a new class which extends JParameter and then cast my $params object down to this new sub class.

// 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

posted date: 2008-12-22 17:15:00

Re: Change the class of an object at runtime#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-22 17:15:01

Re: Change the class of an object at runtime#3
I don't think this is possible in a nice way. (Okay, it's possible, but the way I saw it done was a hack involving serialize() and str_replace(). Probably not what you want considering maintainability is the issue here...)

posted date: 2008-12-22 17:19:00

Re: Change the class of an object at runtime#4
serialize and str_replace? yikes!

posted date: 2008-12-22 17:34:00

Re: Change the class of an object at runtime#5
There's always PECL's Classkit but I get a feeling that you'd really rather not do this. Assuming you're directly calling $this->params->render(), you might just want to make a function/object that does an alternate rendering ( MyParamRenderer::render($this->params)) and avoid performing OO gymnastics not natively supported by the language.

posted date: 2008-12-22 18:00:00

Re: Change the class of an object at runtime#6
yeah I think that's definitely a better option. Also, I wish I could give +2 votes for an answer which acknowledges that not everyone wants to install every single library for the smallest thing!

posted date: 2008-12-22 18:13:00

Re: Change the class of an object at runtime#7
What about creating a decorator of sorts that delegates anything apart from JParameter::render() to the existing objectclass MyJParameter { private $jparm; function __construct( JParameter $jparm ) { $this->jparm = $jparm; } function render() { /* your code here */ } function __get( $var ) { if( isset( $this->$jparm->$var ) { return $this->$jparm->$var; } return false; } function __set( $var, $val ) { /* similar to __get */ } function __call( $method, $arguments ) { if( method_exists( $this->jparm, $method ) { return call_user_func_array( array( $this->jparm, $method ), $arguments ); } return false; }}Or is this just too smelly?

posted date: 2008-12-23 01:58:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.