0

Possible Duplicate:
Undeclare a class in PHP

There are class A in A.php, and B in B.php. I create a new instance of B in one of functions of A. In that function I change the B.php file and it's structure, I want to use the new B.php in continue lines of A.php.

If I call require 'B.php' it says Fatal error: Cannot redeclare class B

Is there any way to remove 'class' B from memory and require it again?

I'm using an ORM named Doctrine I have a form builder that generates Files for Entities so When the form submits I have to change an Entity's file and require it again and also use the new object! This is my actual problem

Or Is there any way to include a file as another name like python? in python we say import sys as MyName

Community
  • 1
  • 1
sinoohe
  • 40,334
  • 3
  • 19
  • 16
  • 1
    This sounds like a poor design, especially if you're asking about forcing PHP to redeclare a previously defined class. You may want to reconsider your approach. – nickb Feb 07 '12 at 13:19
  • 1
    changing the code of one php script by another seems very strange to me. do you realy **need** it? – k102 Feb 07 '12 at 13:20
  • Why would you want to do that? And how would you like to remove all these instances of `B` and all classes inheriting from `B`, plus all their instances? This is very poorly written piece of software, unless you will explain the reasons for doing this. – Tadeck Feb 07 '12 at 13:20
  • You should explain what you want to do. Maybe there is a more appropriate solution than this one. – Matthieu Napoli Feb 07 '12 at 13:24
  • 2
    I wouldn't jump on someone and declare their software poorly written until they have explained their reasons. I write "bad code" all the time, just to see if something works, to experiment, and test the limits of the language. – Leigh Feb 07 '12 at 13:24
  • @Leigh Thank you finally someone that makes sense. – Matthieu Napoli Feb 07 '12 at 13:26
  • @Leigh: It is possible, but if this happens in a simple application, it would be hard for me to believe it is well written. Although I would be happy to hear what is the reason for what OP wants to achieve. No disrespect. – Tadeck Feb 07 '12 at 13:27
  • @sinoohe: What is it you actually need to change in the class? Is it really so dynamic it can't be set up during the constructor, or using a factory method? – Leigh Feb 07 '12 at 13:41
  • @Leigh I have a form builder using Doctrine, I want to generate entity files or change them, Every Entity file referes to a class so If I change a file tha class that that links to will be changed! The problem is in the code generator section that I'm using that class that I'm changing the file of that!!! – sinoohe Feb 07 '12 at 14:22
  • @sinoohe It sounds to me like you need to implement a factory class. – Leigh Feb 07 '12 at 14:30

2 Answers2

0

Maybe there are classes the same on both you a.php and b.php,

tomexsans
  • 4,454
  • 4
  • 33
  • 49
-5

You have to use the function require_once('B.php') or include_once('B.php') this way it wont send warning. There i sno need to remove the class from the memory. WP uses this functions.

http://cz2.php.net/include_once

http://cz2.php.net/require_once

Hristo Petev
  • 309
  • 3
  • 13
  • -1 No, read the question. Not only is your answer incorrect, it does not help the OP at all. – nickb Feb 07 '12 at 13:19
  • 1
    Dude why you do vote -1, the problem is not the design. OK. Its the code – Hristo Petev Feb 07 '12 at 13:20
  • 3
    Because not only is your answer false (both functions will raise the SAME error that the OP posted), it doesn't answer the original question, hence a loving -1 vote. – nickb Feb 07 '12 at 13:23
  • 7
    And I think it's hilarious that you think I'd care that you viewed my profile and downvoted my top questions and answers on StackOverflow. Oh, the maturity. Bravo. – nickb Feb 07 '12 at 13:25