Jump to content

change class of an object?


mbeals

Recommended Posts

Is it possible to change the class of an object if it retains the same parent?

 

IE

 

<?php

class device {

 

Can you be more specific? Changing a class name in the definition of an object is simple enough, but it will break all the code that is instantiating the object by its previous name. Why would you want to change the class name of an object?

sorry,

 

I was attempting to tab over while writing code (which doesn't work in this box) and submitted the topic before it was complete.

 

I was going to ask if you have an object which is a child of another (or even of an abstract), is it possible to change the child objects class, retaining the abstracted attributes.

 

Effectively taking an object of one class, destroying it, then creating a new instance of another class (with the same parent) and persisting the common attributes.

 

ie:

 

<?php
class parent{

  protected $id;
  protected $info;

  function changeClass($newClass){

  }

}

class child1 extends parent{

  function __construct(){

  }

}

class child2 extends parent{

}


$test = new child1;
$test->changeClass('child2');
?>

 

 

where any attributes defined through child one that are common with child2 would persisit.

 

Could I just do something like:

 

<?php


function ChangeClass($newClass){
   unset($this);
   return new($newClass);
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.