How can i "unset" a class that has been declared
Asked Answered
F

4

8

I have a complete Form Creation System. There are many different Question Types and I have to check if the answer which the person had made is correct.

All types of Questions has its own class will is always called ItemClass and this Class is in its own File.

Everytime a Question is asked I include the File and there is the problem: I have to check different types of Questions in one PHP-File. And then there comes the error - ItemClass already declared.

Is there any method to unset a Class?

Freytag answered 10/2, 2010 at 11:12 Comment(0)
F
1

I tested your solutions and finally I got an Idea: I renamed the Classes to each of it's type (ynItemClass, markItemClass, ...) and when I need a Item Type a made the following

$it = $item->item_type."ItemClass";
$test = new $it($smarty);

And it works!!

Have a nice Day! Dominik

Freytag answered 17/2, 2010 at 7:46 Comment(0)
B
7

No, it is not possible to "undefine" an existing class.

In your case, you should not have several classes that all have the same name : each class should have a different / distinct name, and you should modify the way you are working with those, so your code deals with classes not named ItemClass.


For instance, you could have :

  • ItemClass_Type1 in itemclass_type1.php
  • ItemClass_Type2 in itemclass_type2.php

and so on -- and those classes could all extend the same base class, if needed / if it makes sense.


(For a while, I thought maybe runkit could help with the "undefining a class" idea ; but there doesn't seem to be a function to do that -- and that extension is not quite stable and shouldn't be used on a production server)

Benign answered 10/2, 2010 at 11:26 Comment(0)
N
6

No.

Once a class is decalred it cannot be 'unset'

Maybe namespaces could be of use to you?

Natatorium answered 10/2, 2010 at 11:15 Comment(1)
Mhm I am not sure I can use that for my problemFreytag
P
2

No. You have a design flaw.
You should have probably let the different type of questions inherit from one abstract question class and/or (1) used the factory pattern or (2) used the strategy pattern.

Phoneme answered 10/2, 2010 at 11:39 Comment(0)
F
1

I tested your solutions and finally I got an Idea: I renamed the Classes to each of it's type (ynItemClass, markItemClass, ...) and when I need a Item Type a made the following

$it = $item->item_type."ItemClass";
$test = new $it($smarty);

And it works!!

Have a nice Day! Dominik

Freytag answered 17/2, 2010 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.