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

__construct arguments

Discussion in 'Facepalm' started by Zayd, Jun 17, 2017.

  1. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    Error:
    PHP:
    16.06 22:43:59 [ServerServer thread/CRITICAL TypeError"Argument 2 passed to Zayd\Other\EventHandler::__construct() must be an instance of Zayd\Other\Items, none given, called in phar:///plugins/ComboCore_v2.0.0 (4).phar/src/Zayd/Main.php on line 49" (EXCEPTIONin "/ComboCore_v2.0.0 (4).phar/src/Zayd/Other/EventHandler" at line 36
    EventHandler Class:
    PHP:
    namespace Zayd\Other;

    use 
    Zayd\Other\Items;
    use 
    Zayd\Main;

    class  
    EventHandler implements Listener {
        
        public function 
    __construct(Main $mainItems $items) {
           
    $this->main $main;
           
    $this->items $items;
               }
        
        public function 
    getPrefix(){
            return 
    $this->main->prefix;
                 }
        
                 public function 
    getItems(){
                     return 
    $this->items;
                 }
    Items Class:
    PHP:
    namespace Zayd\Other;

    use 
    Zayd\Main;

    class 
    Items extends Item {
        
       public function 
    __construct(Main $main) {
           
    $this->main $main;
       }
    Folder Structure:
    src->Zayd->Other->Items.php
    src->Zayd->Other->EventHandler.php

    No idea why this is happening?
     
    Miste likes this.
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Your Items class constructor needs to have the same parameters as the Item class. It also needs a parent constructor call. This has nothing to do with PocketMine API. Learn PHP or put questions like this in the facepalm section.
     
    Zayd likes this.
  3. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    My Item's class constructor meaning the constructor in EventHandler?
    Please give me an example.
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    How are you creating the EventHandler object? Also, your Items class doesn't have a use statement for pocketmine\item\Item.
     
  5. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    It's just part of the class.
     
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    How are you creating the EventHandler object inside your Main class?
     
  7. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Actually, only classes that extend another need a parent constructor call :p Implements implements an interface, which don't have (filled) constructors
     
  8. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    I'm not creating it, I'm just registering events in the main class.
     
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    where's the main file?
    you also completely missed the error,
    Code:
    class Door {pub func __construct(Owner $owner, Key$key){}}
    $door = new Door(new Owner("thunder"));
    
    your mistake is forgetting to give the "Key" in this example
    this would result in something like "Argument 2 passed to Door::__construct() must be an instance of Key, none given, called in now here"
    to fix it just do
    Code:
    $door = new Door(new Owner("thunder"),new Key("1337"));
    
    in a general sense however you are meant to construct that said object will work
     
  10. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    If you weren't creating an instance of that class, you wouldn't be getting that error. Learn about objects if you don't quite understand.

    Can we see the code in your main class where you register the listener?
     
  11. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    PHP:
    use Zayd\Other\EventHandler;

    public function 
    onEnable(){
    $this->getServer()->getPluginManager()->registerEvents(new EventHandler($this), $this);
    }
     
  12. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    The EventHandler class wants to access functions from the Items class, basically
     
  13. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You have to have an Items object for the second parameter in "new EventHandler()". This will resolve your error and you will then be able to use it.
     
  14. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    Ahh. So which parenthesis though? Example please, that's really confusing
     
  15. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I don't quite know what you mean with parenthesis, but I meant something like this:
    PHP:
    use Zayd\Other\EventHandler;

    public function 
    onEnable(){
    $this->getServer()->getPluginManager()->registerEvents(new EventHandler($this, new Items($this)), $this);
    }
    Again:
     
  16. Zayd

    Zayd Witch

    Messages:
    55
    GitHub:
    ZaydPE
    thankyou
     
  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.