SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    undeclaring a class

    hi to all,
    i am going to ask a pretty weird question. i've figured out a few ways round it, but now it's become a personal cthing and i'd like to know if...

    ...is it possible to "undeclare a class"?
    ie can i declare a class "foo", undelcare it and declare a different class with the same name "foo".
    it may sound a pretty pointless question, but it is actually very important to me. thank you all for your help.

    ciao
    frodo

  2. #2
    SitePoint Zealot lemkepf's Avatar
    Join Date
    Feb 2002
    Location
    WI, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you provide a little information about why you want to do this?
    Happiness is a belt-fed weapon
    Consciousness: That annoying time between naps.
    Modevia Web Services LLC.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your object is $foo you can use

    unset($foo);

    Pretty sure in PHP 5 there will be built-in a 'destruct' or 'destroy' method for classes.

  4. #4
    SitePoint Zealot lemkepf's Avatar
    Join Date
    Feb 2002
    Location
    WI, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh man... was i reading that question wrong.... i was thinking you wanted to do something like this:
    PHP Code:
    //first foo
    class foo(){

    }

    //undeclare foo
    undeclare foo;

    //create new foo
    class foo(){


    Was i way off
    Happiness is a belt-fed weapon
    Consciousness: That annoying time between naps.
    Modevia Web Services LLC.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think you aren't off

    ie can i declare a class "foo", undelcare it and declare a different class with the same name "foo".
    But i don't know why someone woult want to do this, and not how

  6. #6
    SitePoint Zealot lemkepf's Avatar
    Join Date
    Feb 2002
    Location
    WI, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, ok, if i'm not off so bad.... i believe if you just have the class "foo" later in the code it should overwrite the first "foo". No time to test.... can someone try it?
    Happiness is a belt-fed weapon
    Consciousness: That annoying time between naps.
    Modevia Web Services LLC.

  7. #7
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no need to try, you'll get something like "Cannot redeclare class Foo in file.php on line whatever". Also seen when you include a classfile in more than one place.

  8. #8
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guess I read the question wrong. I thought frodo was just talking about an instance of a class.

  9. #9
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    may be just extend the existing class. In that way you can 'overwrite' some methods (but obviously originally declared variables and methods not overwritten will still be there)

  10. #10
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is not possible to substract from a class, that is, to undefine any existing functions or variables.
    -php manual

  11. #11
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PeterW
    It is not possible to substract from a class, that is, to undefine any existing functions or variables.

    -php manual
    I don't think frodo was asking how to 'subtract from a class', maybe he can clarify what he's trying to do.

  12. #12
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for all your help

    what i am trying to do is really simple, if only an exercise for my own intellectual benefit. all i am trying to do is to "undeclare" a class i have already used.

    if i "declare"
    Code:
    class Template {...}
    all i would like to do is to find a function which destroys this class and allows me to redeclare the same class a second time. pretty stupid, but this problem has been bugging me for a week now!!

  13. #13
    SitePoint Enthusiast Viper_SB's Avatar
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no it's not possible and shouldn't be needed, i.e you shouldn't ever have a need for that, if you explain why you think you do maybe we can help you see it from a different side.

  14. #14
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viper_SB
    no it's not possible and shouldn't be needed, i.e you shouldn't ever have a need for that, if you explain why you think you do maybe we can help you see it from a different side.
    He said in the first post that he had already found a solution to his specific problem... he is looking for a general solution.

    I don't have an answer, but seeing 5 people answer the wrong question forced a reply

    Perhap if the "old" class definiftion was included in an external file, some code could be written to change which file is included (and hence include your new class instead) though that won't help you redefine a class surrently defined in memory.

    General Note: if you ever want to have two objects which you would like to interchange, give them a common interface, then program to that interface.

    for example:

    PHP Code:
    class alpha {
      function 
    do_something($param) { ... }
    }

    class 
    beta {
      function 
    do_something($param) { ... }
    }

    class 
    worker {
      
      private 
    $childclass;
      
      function 
    __construct($childclass) {
        
    $this->childclass $childclass;
      }

      function 
    do_some_stuff() {
        ...
        
    $param 6;
        ...
        
    $this->childclass->do_something($param);
        ...
      }
    }

    $my_first_worker = new worker (new alpha());
    $my_other_worker = new worker (new beta()); 
    This pattern has a name, but I've forgotten what it is!

    Later,
    Douglas
    Hello World

  15. #15
    SitePoint Enthusiast Viper_SB's Avatar
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya I know he found a solution but he said he was wondering that's why I asked, so we could see what exactly he was looking for.

    as for your code I think it's just called Interfaces also used with Abstraction

  16. #16
    SitePoint Zealot lemkepf's Avatar
    Join Date
    Feb 2002
    Location
    WI, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wasn't wrong the first time just wrong the second time... besides... i just had a 2 hour long interview and i'm fried.... so blorp
    Happiness is a belt-fed weapon
    Consciousness: That annoying time between naps.
    Modevia Web Services LLC.

  17. #17
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viper_SB
    as for your code I think it's just called Interfaces also used with Abstraction
    Just looked it up: in GoF, it is the strategy pattern, though yes, that it how it works.

    Theoretically we could have a higher level discussion than the one at the moment, and my post above would have been "I don't know how to do that, though it sounds like you are trying to force the strategy pattern in the implementation rather than in the interface", but I'm not sure how many people would understand that. I probably wouldn't have a month ago.

    Reading books == informative.

    Douglas
    Hello World


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •