21

PHP ist one of the languages I use regularly, but not the main language.
Anyhow, passing an array to a function will create a Copy of the array unless you specifically choose to Pass the reference.
That's seriously fucked up. What other language does that?! Coming from C, Java, Python to PHP I was not prepared to expect shit like that.

Comments
  • 1
    There's a con talk from the guy who wrote php that will likely make you lose all hope. I'll see if I can find it.
  • 1
  • 3
    Golang
  • 8
    I like it. I'm not affecting the global state of the array unless I explicitly want to, it's a tad safer and I can cleanup the data in the cloned array from memory when I no longer need it.
  • 4
    @SortOfTested "I've always been firmly against any sort of multi threading because it just gets too complicated. We as humans are not smart enough to write complex multi thread safe code"

    Eh
  • 0
    DO NOT USE PHP UNDER PENALTY OF THERMAL RUNAWAY CAUSED BY HCF CAUSED BY YOUR SHIT INTEL SERVER CAUSED BY INITIAL PHP HAX
  • 4
    @C0D4 Sounds Like a severe Case of Stockholm Syndrome
  • 6
    @don-rager could be, Ive been in the abusive relationship for over a decade 🥰

    I do use python and java as well and have no issue with their way of handling data, I just enjoy that bit of freedom/control depending how you look at it.
  • 10
    What @C0D4 said.

    PHP is not Rust.

    You don't use PHP to effectively calculate digits of pi, sort through big piles of image data or simulate weather patterns.

    People use PHP as a transformation layer between an HTTP request and a database.

    PHP arrays are incredibly inefficient to begin with, and them being immutable by default doesn't help -- but they aren't meant for efficiency.

    In practice, no one uses straight up arrays, you use some kind of Collection object (which operates on values by reference).

    Also, if you want efficiency, you also have Sets, Stacks, Maps, and Vectors in PHP:

    https://php.net/manual/en/...

    Criticizing PHP for its arrays... As a Haskell developer, looking at Java:

    "WTF what is this Null crap? Why are you throwing exceptions all over the place? What is this garbage?"
  • 5
    @Jilano we can't be having a well formed argument around here 😆

    @bittersweet wait, there's someone else around here in agreement with me on this topic 🥰 I'm not used to this.
  • 2
    @Jilano

    I guess I won't start about my unironic passion for 3-space indentation then.
  • 1
    It's easier to pass value type arrays by reference when needed, compared to passing reference type arrays by value.

    Value type arrays are cleaner and safer, too. No accidental side effects.

    You asked which language does that: Swift does.
  • 1
    @Jilano

    It's pretty simple: Haskell has alignment-based indentation, everything in a block is aligned to a keyword. But in some cases you must still pick an indentation, for example when you put a block of code after a "do" on a new line, or kind of want a reset on the alignment... the only actual compiler-enforced rule is that it must be indented further than the previous line.

    I've worked on a Haskell codebase of a few million lines, which already used 3 spaces in those cases. If you stare long enough at something, you'll eventually start to hate it less, maybe even like it (or so my girlfriend told me).

    Now I find that 2 is kind of ambiguous and subjectively "not indented enough", and 4 feels like "overkill".

    So.... 3 spaces indentation feels right to me now.

    But I would not just push a commit replacing 2 or 4 spaces with 3 for an existing codebase... I only use 3 spaces in private projects.
  • 1
    @ScriptCoded
    I fully disclaimed it before sharing :)
  • 1
    To full appreciate PHP, the photo should show a person with leprosy and nail fungus, holding the hammer with their feet. Seems like most PHP devs I'm forced to pick up after have double digits IQs.
  • 0
    @Jilano I'm already confused damnit 😂😂😂
  • 1
    Actually it's perfectly reasonable PHP does this. To answer your question: Perl, Go.
    What you see in other languages is an implementation detail coming trough. Why are the other data types like int copied and perfectly acceptable to you?
    PHP does this right and consistently. Unless specified you do not want to accidentally modify the data. However you do not need to go wild and & all your parameters!!! For performance reasons the data is only copied when changed.

    Objects are different as you inject them to perform operations on them (although stdclass is not designed for this) they should be passed as reference as this is what you almost always need.
  • 0
    C/C++ works like this also, but you may got confused, because common representation of arrays are pointers in C, but it's almost equal to a reference. So basicly passing an array to a function in C is a reference passing.
  • 0
    @hjk101 Also, in every-day practice, you rarely pass arrays to methods.

    When you retrieve a bunch of users from a database, you probably have them stored in some (symfony/laravel/etc) Collection object, which internally uses a protected array. Laravel even has a LazyCollection class which has a similar public interface to normal Collections, but is completely based on (lazy) generators instead of arrays.

    I would never claim that PHP is good at datastructures. Even the PHPDS vector/set/map/dequeue etc stuff is "passable" at best compared to Haskell or Rust.

    But PHP isn't like some malformed hammer either. It's more like a Swiss army knife. All the tools it offers are not as good as specialized tools, but they are passable.

    However, the best feature of PHP is that you can actually hire teams for it.

    Have you ever tried to hire 25 Rust devs? Or even just a team of proficient C++ devs on a startup budget?
  • 1
    @C0D4 you are never alone boo, php fam unite!
Add Comment