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

How to Use Array in Function

Discussion in 'Development' started by byyEmirhanWSD, Mar 4, 2018.

  1. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    How to write this?

    CODE:
    public function blala(Player $player, array $items, int $id, string $title) {

    }

    PHP:
    // I used this but i gave error.
    $plugin->blala($player, [12], 54"Test");
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    What was the error
     
  3. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    What object(class) are you referring to in $plugin?
     
  4. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
    $plugin->blala($player, array(1,2), 54, "Test");
     
  5. TheDragonRing

    TheDragonRing Witch

    Messages:
    64
    GitHub:
    dragonwocky
    Other devs correct me if I'm wrong, I haven't messed with PHP in a while - but why are you defining types for everything, even strings and numbers? You should just be able to do:
    PHP:
    public function blala(Player $player$items$id$title) {
      
    // do something
    }
    blala($player, array(1,2), 54'Test');
    // $player === $player
    // $items === array(1,2)
    // $id === 54
    // $title === 'Test'
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Type-hinting is a good practice and decreases the chances of bugs in your code.
     
  7. TheDragonRing

    TheDragonRing Witch

    Messages:
    64
    GitHub:
    dragonwocky
    A good practice, sure. But if trying to avoid bugs by using type-hinting is actually causing the bugs... You should at least test it without the type-hinting to see if that fixes it.
    I've mainly just been doing JS for (almost) a year, so I guess I could be looking at this with slightly different logic to everyone else, but still...
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    if it causes bugs, fix the new bugs.
     
  9. TheDragonRing

    TheDragonRing Witch

    Messages:
    64
    GitHub:
    dragonwocky
    Sorry, I wasn't very clear there. I meant that you should test everything (including without type-hinting) so that you can see what really is causing the bugs, so that you actually can fix those bugs.
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Typehinting checks if the value is of that type at runtime. What is the point of testing without typehinting?
     
  11. TheDragonRing

    TheDragonRing Witch

    Messages:
    64
    GitHub:
    dragonwocky
    Oh. I guess I misunderstood what type-hinting was then... Sorry.
    The point of testing without type-hinting would be to run it, and log what values are passed to it without type-hinting so you can see what it actually is receiving, and what is being done with what it is receiving. And it would be partly out of curiosity, on my part at least, to see what happens.
    I would test everything, necessary or not, just to see what things are doing (code rarely does exactly what I expect it to), but that's just me. Others may view things differently ¯\_(ツ)_/¯

    Since the OP hasn't given us the error yet, that may or may not help. Just suggesting what I could from my limited knowledge/experience. The error is probably to do with something inside that function, not the type-hinting, but all he gave us was the type-hinting...
     
  12. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    This thread has went off-topic...
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The thread should be a Facepalm one by itself :p
     
    TheDragonRing and Daniktheboss like 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.