1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

PocketMine ZipArchive use?

Discussion in 'Development' started by HittmanA, Nov 21, 2016.

  1. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    I am making this new plugin and long story short it needs to be able to uncompress files. I found how to do that with PHP, but for some reason I can't use it in a PocketMine plugin. Here is the de-compressing code I am using:
    PHP:
    $zip = new ZipArchive;
            
    $res $zip->open("foo.zip");
            if (
    $res === TRUE) {
                
    $zip->extractTo("../bar");
                
    $zip->close();
                
    unlink("foo.zip");
                
    $sender->sendMessage("Foo has been installed successfully!");
            } else {
                
    $sender->sendMessage("Foo has failed to install!");
            }
    And here is the error:
    Code:
    2016-11-21 [12:08:43] [Server thread/CRITICAL]: ClassNotFoundException: "Class HittmanA\pmpm\ZipArchive not found" (EXCEPTION) in "/src/spl/BaseClassLoader" at line 144
    (Line 144 would be the
    PHP:
    $zip = new ZipArchive;
    line)
    How can I use ZipArchive in PocketMine?
     
  2. svile

    svile Spider

    Messages:
    14
    add a backslash before the class name , like:

    PHP:
    $zip = new \ZipArchive();
    and should work. if not , you need the PECL zip
     
    HittmanA likes this.
  3. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    ZipArchive is a native PHP class, not one that I made myself so do I need to include the \ or \HittmanA\pmpm\ZipArchive. Also the plugin name is pmpm.
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    just do
    $obj = new \ExampleObj();
    since it is a build in class
    like
    said so
     
    HittmanA likes this.
  5. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    Ahhhh ok I see now. Thank you!
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The backslash is because the class is in the root namespace. If you don't add a backslash, PHP will understand it that the class is in your current namespace.

    Alternatively, you can add a use statement just like other classes:
    PHP:
    use ZipArchive;
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Native/builtin classes have no necessary relationship with using the top namespace.
    Where is the votedown button?
     
    Thunder33345 likes this.
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I can still qualify \my\clazz\names\like\this instead of using class aliases. And I like to import top namespaces so that I don't need to type the backslash all the time. There is no relationship between the namespace and whether I should fully qualify the class with a leading backslash or use a use statement.
     
    HittmanA likes this.
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you are meant to add it
    dont look at me like that, i cant
     
    HittmanA likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.