Functions how to pass variables

C++, C#, Java, PHP, ect...
Post Reply
Zykal
Posts: 31
Joined: Thu Nov 29, 2012 4:38 am

Functions how to pass variables

Post by Zykal »

I want to clean my code up and turn a lot of things into functions however that means passing variables.

How should I pass variables to functions I know PHP supports passing variables via arguments as value by default

if i pass a variable to a function and in the function that variable is changed (i want it to change)
Is the best way to just pass the variable to the function as a reference argument?

is there any issue with passing them as reference? like there is as using them as globals?

Also is it worthwhile to pass all MYSQL array data as variables instead of re-pulling the info in the function?

any hints tips or tricks?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Functions how to pass variables

Post by Jackolantern »

The general rule of thumb with functions is that if you are doing lots of passing arguments by reference you are probably making your code difficult to debug and maintain. I have also heard that there are several open bugs in passing-by-reference in PHP since it is really just an official hack in the system that many users are calling for deprecation of.

Instead, I would suggest to read a few tutorials on object-oriented development in PHP. That will go miles further to clean-up your code and provide good structure. Since an entity's state and the behaviors to operate on that state are in the same container, you can essentially remove the need for passing variables by reference, except in some rare and isolated instances. Instead, objects can simply expose methods (functions in classes) to change their internal state, and other objects can call those methods as needed. So the state will change inside the object, instead of in an unrelated function :)
The indelible lord of tl;dr
Zykal
Posts: 31
Joined: Thu Nov 29, 2012 4:38 am

Re: Functions how to pass variables

Post by Zykal »

Speaking of hacks I thought OOP in PHP was a hack. I take it they officially brought it in with PHP5?
I'm not even sure where to start with that.

Using halls mmo script as an example what would you oop?

I understand oop for certain things but I've seen it make things worse.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Functions how to pass variables

Post by Jackolantern »

It was officially added into the language in PHP 5. In PHP 4 it was essentially an after-thought and was pretty bad. Now it has pretty good OOP support.

Definitely a good candidate to be turned into a class would be player. Also monster, and then probably several other things. Maybe inventory (that could perhaps be contained within player), maps, etc.

And bad OOP design can make things worse, but from what I have seen you really have to go out of your way to make it worse. While procedural PHP seems easier and more manageable when it is being written, it is extremely hard to go back and add new features later when everything is hopelessly tangled together. :cool:
The indelible lord of tl;dr
Post Reply

Return to “Coding”