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?

Link to comment
Share on other sites

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);
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.