When is it good to use pass by reference in PHP?

前端 未结 3 2027
悲&欢浪女
悲&欢浪女 2020-12-02 09:53

In C++ if you pass a large array to a function, you need to pass it by reference, so that it is not copied to the new function wasting memory. If you don\'t want it modifie

3条回答
  •  日久生厌
    2020-12-02 10:33

    In PHP :

    • objects are passed by reference1 -- always
    • arrays and scalars are passed by value by default ; and can be passed by reference, using an & in the function's declaration.


    For the performance part of your question, PHP doesn't deal with that the same way as C/C++ ; you should read the following article : Do not use PHP references


    1. Or that's what we usually say -- even if it's not "completely true" -- see Objects and references

提交回复
热议问题