SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Unloading a class

    Is it possible to unload a class? If so, what's the keyword?

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Why would you ever want to do such a thing?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $o = new object();
    $o null
    There you go. Once all references to the object are removed its gone.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    The class definition would still exist though...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Why would you ever want to do such a thing?
    I'm using two versions of a class that was provided by a vender that may not have intended that they be used together. I've requested clarification. In the meantime, I thought it might be possible to unload/unload the versions with code.

    Is it possible to rename a class after it's loaded?

  6. #6
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,259
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    My suggestion would be to ever so slightly alter the vendor code to wrap it in namespaces.
    "First make it work. Then make it better."

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I looked at the manual. If I understand it correctly, there's a way to encapsulate the require statements so they can still be used in this code. How do I use example #1 at http://www.php.net/manual/en/languag....rationale.php to encapsulate the require statements in this code?
    PHP Code:
    <?php
    require('BCGColor.php');
    require(
    'BCGDrawing.php');
    require(
    'BCGqrcode.barcode2d.php');

    // QRCode Part
    $code = new BCGqrcode();
    $code->setScale(4);
    $code->setErrorLevel(1);
    $code->parse('Code 2D!');
     
    // Drawing Part
    $color_black = new BCGColor(000);
    $color_white = new BCGColor(255255255);
    $drawing = new BCGDrawing(''$color_white);
    $drawing->setBarcode($code);
    $drawing->draw();
     
    header('Content-Type: image/png');
     
    $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

    ?>

  8. #8
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,259
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nichemtktg View Post
    If I understand it correctly, there's a way to encapsulate the require statements so they can still be used in this code.
    I couldn't find where the manual demonstrated wrapping a namespace around a require statement. Nonetheless, I gave it a try, but it didn't seem to work.

    You'll probably have to open each of the vendor files and at the top, add a line similar to this:

    namespace VendorName\LibraryName\VersionX;
    "First make it work. Then make it better."

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,119
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    I couldn't find where the manual demonstrated wrapping a namespace around a require statement. Nonetheless, I gave it a try, but it didn't seem to work.

    You'll probably have to open each of the vendor files and at the top, add a line similar to this:

    namespace VendorName\LibraryName\VersionX;
    Another approach:
    PHP Code:
    require('BCGColor.php'); 
    require(
    'BCGDrawing.php'); 
    require(
    'BCGqrcode.barcode2d.php');

    namespace 
    VendorName\BCGColor\v1;
    {
        class 
    VendorColor extends BCGColor
        
    {
        }
        class 
    VendorDrawing extends BCGDrawing
        
    {
        }
        class 
    VendorQRCode extends BCGqrcode
        
    {
        }

    You would then use new VendorColor(), etc in your code instead of BCGColor, etc.

    Assuming the vendor classes are not marked as final

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,259
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    You would then use new VendorColor(), etc in your code instead of BCGColor, etc.
    Well... almost. Actually you would have VendorColor in addition to BCGColor. The reason that distinction is important is because we would still need to load a different version of the same library, and if that different version tries to re-declare classes, such as BCGColor, then PHP will throw a parse error.
    "First make it work. Then make it better."

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,119
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    Well... almost. Actually you would have VendorColor in addition to BCGColor. The reason that distinction is important is because we would still need to load a different version of the same library, and if that different version tries to re-declare classes, such as BCGColor, then PHP will throw a parse error.
    Wouldn't you load them under two different namespaces?

    PHP Code:
    require('BCGColor.php');  
    require(
    'BCGDrawing.php');  
    require(
    'BCGqrcode.barcode2d.php'); 

    namespace 
    VendorName\BCGColor\v1

        class 
    VendorColor extends BCGColor 
        

        } 
        class 
    VendorDrawing extends BCGDrawing 
        

        } 
        class 
    VendorQRCode extends BCGqrcode 
        

        } 

    PHP Code:
    require('BCGColor2.php');  
    require(
    'BCGDrawing2.php');  
    require(
    'BCGqrcode.barcode2d2.php'); 

    namespace 
    VendorName\BCGColor\v2

        class 
    VendorColor extends BCGColor 
        

        } 
        class 
    VendorDrawing extends BCGDrawing 
        

        } 
        class 
    VendorQRCode extends BCGqrcode 
        

        } 

    And then you could use both
    PHP Code:
    $version1 = new VendorName\BCGColor\v1\VendorColor();
    $version2 = new VendorName\BCGColor\v2\VendorColor(); 

  12. #12
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    cpradio's method will work unless you do need two variants of the class loaded at the same time. You won't be able to define two different instances of the class with the same name.

    Another (hacky way) would be to load a file into a namespace:



    PHP Code:
    function loadIntoNamespace($file$namespace) {
        eval(
    'namespace ' $namespace '; ?>' file_get_contents($file));    
    }


    loadIntoNamespace('BCGColor.php''Version1');
    loadIntoNamespace('BCGColor.2.php''Version2');

    $v1instance = new Version1\BCGColor();
    $v2instance = new Version2\BCGColor(); 

    Really, this should be included functionality in the language as it is useful at times. Especially in the transitional period between never having namespaces available and older code not using them and transitioning into new code which uses them.

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,119
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    cpradio's method will work unless you do need two variants of the class loaded at the same time. You won't be able to define two different instances of the class with the same name.

    Another (hacky way) would be to load a file into a namespace:



    PHP Code:
    function loadIntoNamespace($file$namespace) {
        eval(
    'namespace ' $namespace .'; ' file_get_contents($file));    
    }


    loadIntoNamespace('BCGColor.php''Version1');
    loadIntoNamespace('BCGColor.2.php''Version2');

    $v1instance = new Version1\BCGColor();
    $v2instance = new Version2\BCGColor(); 

    Really, this should be included functionality in the language as it is useful at times. Especially in the transitional period between never having namespaces available and older code not using them and transitioning into new code which uses them.
    That is a neat approach.

  14. #14
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    It works, certainly. But I wouldn't recommend it unless there's no other way. Any debugging information from the included files will display errors on lines in eval()'d code, making it hard to track down where notices/warnings/exceptions are coming from.

  15. #15
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,259
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Wouldn't you load them under two different namespaces?

    require('BCGColor.php');
    require('BCGDrawing.php');
    require('BCGqrcode.barcode2d.php');

    ...

    require('BCGColor2.php');
    require('BCGDrawing2.php');
    require('BCGqrcode.barcode2d2.php');
    The problem is PHP will throw a parse error at your second set of require statements, because they would be re-declaring classes that were already declared in the first set of require statements.
    "First make it work. Then make it better."

  16. #16
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,119
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Oh, yeah, I guess they would. I keep forgetting that about PHP and working with Third Parties (as namespaces aren't required).


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
  •