Jump to content

Calling a function from inside a function - possible ?


MacRedrum

Recommended Posts

I have a .php file that has my main function and some help functions to keep the main function cleaner.

Like this:

<?
function sortArray();
{ 
-global variables;
-for loop sorting out $input-array
}

function mainProgram($input);
{
global variables;
sortArray();
}
?>

 

It does give some warnings like: "Warning: Illegal offset type". Which go away when I fiddle around with the includes.

The main problem seems to be, that the variables don't seem to transfer back to the main function. I've tested the sortArray() seperately and it works. But even in the seperate test file, when I put the call inside a function, it again didn't transfer any data.

 

So is it even possible or is there some trick to doing it ? I was hoping to make the main function available with a call. It was so messy when I had all the side functions straight in the main code. In that form it's around 1300 lines long, so everyone can imagine how hard it's to troubleshoot :). With the functions it's under 200 lines long.

 

Link to comment
Share on other sites

Please show us the real code.

 

Ken

 

Alrighty. I'll still cut some things, since like I said it's over 1200 lines long.

 

function sortInputData()
/*Moving data from $input to new tables and variables */
{
global $input;
global $attShipsAlive;
global $attShipsRemaining;
global $defShipsAlive;
global $defShipsRemaining;
global $attSize;
global $attPlanets;
global $defTelleriumTick;
global $defKryptonTick;
global $defMines;
global $defProbes;
global $defSize;

for($inputs=1; $inputs<=47; $inputs++)
{
if($inputs<=17)
{
	$attShipsAlive[$inputs]=$input[$inputs];
	$attShipsRemaining[$inputs]=$input[$inputs];
}
elseif($inputs==18)
{
	$attSize=$input[$inputs];		
}
elseif($inputs==19)
{
	$attPlanets=$input[$inputs];
}
elseif($inputs>=20 && $inputs<=42)
{
	$defShipsAlive[$inputs]=$input[$inputs];
	$defShipsRemaining[$inputs]=$input[$inputs];
}
elseif($inputs==43)
{
	$defTelleriumTick=$input[$inputs];
}
elseif($inputs==44)
{
	$defKryptonTick=$input[$inputs];
}	
elseif($inputs==45)
{
	$defMines=$input[$inputs];
}
elseif($inputs==46)
{
	$defProbes=$input[$inputs];
}
elseif($inputs==47)
{
	$defSize=$input[$inputs];
}
}
}

.
.
.
~10 other functions
.
.
.

function battleEngine($input) 
{
include_once 'stats.php';
global $input;
global $result;
global $stats;
global $gunPool;
global $accuracy;
global $shipID;
global $attShipAlive;
global $attShipRemaining;
global $attShipEmped;
global $attShipStolen;
global $attLostBuffer;
global $attEmpedBuffer;
global $attStolenBuffer;
global $defShipAlive;
global $defShipRemaining;
global $defShipEmped;
global $defShipStolen;
global $defLostBuffer;
global $defEmpedBuffer;
global $defStolenBuffer;
global $targetShipAmounts;

sortInputData();

"battle stuff"

sortOutputData(); // This just sorts the calculated data to 1 array so it can be returned. Does seem to have the same problem though.
return $output;
}

 

As the names might give it away, I'm doing a battle engine for a browser game. For some reason, those sortXdata functions won't share their variables/data. If I copy the insides to the main function, it works. But since there's those other over 10 functions that process data too, I'm fearing the same problem might be in them too.

 

and to the return comment: That global variable should do the same. Atleast when I tested it. The sort function needs to make multiple arrays and variables. It would be impossible to then return them. These are values that are needed for all functions.

Link to comment
Share on other sites

function battleEngine($input)//1.

{

include_once 'stats.php';

global $input;//2. see something familiar?

 

It might be wise to learn PHP and proper coding standards first. The problems you encounter is due to the use of globals, your data is messed up and your result is with a great deal of good luck what you expect.

Link to comment
Share on other sites

It seems I have to repeat myself. The code without functions is 1300 lines long. It is pretty hard to make sense of that way... I might be able to understand it, but others wont and looking for errors is a pain. In functions it's easier maintain too. As for the parameters. I need the functions to be able access/modify the variables. I know I could pass them as variables, but that wouldn't be that handy either, I was thinking that atleast in theory it should work with globals. I just need to know if it's possible or do I have to integrate the functions back to the code.

 

It might be wise to learn PHP and proper coding standards first. The problems you encounter is due to the use of globals, your data is messed up and your result is with a great deal of good luck what you expect.

Ignace:

I was expecting some help, not smart remarks about how I suck :P. Might of been my bad, for expecting people in the internet to be helpful. Kind of harsh analysis on me that :). I'm learning as I go on since I've never used php to this extent. This will be the 1st working version of the code. After that I will refine it. But I'm sure you did all by the standards when you were learning... right ? Hard to learn if you are afraid of being wrong or testing things.

 

But to the point:

Were you trying to say that I can't have the same variable in my function parameters and as a global variable ?

Link to comment
Share on other sites

The point of functions is to package code into small encapsulated, reusable, callable pieces of code. Globals break 2 of the three things functions set out to do, encapsulation and re-usability.

 

They also (as you are now experiencing) make things very difficult to debug.

Link to comment
Share on other sites

But I'm sure you did all by the standards when you were learning... right?

 

My comment was simple advice not meant to be harsh or to put you down or anything. Globals are a no-go (if you read up on good coding practices you'll find that to be true). And as you mentioned that you want it to be easily maintainable then... well.. we'll talk later :) as globals make for anything but easy to maintain.

 

Were you trying to say that I can't have the same variable in my function parameters and as a global variable?

 

Sure you can, however the global overwrites whatever $input contained.

 

Can you explain what these do?

 

global $input;
global $attShipsAlive;
global $attShipsRemaining;
global $defShipsAlive;
global $defShipsRemaining;
global $attSize;
global $attPlanets;
global $defTelleriumTick;
global $defKryptonTick;
global $defMines;
global $defProbes;
global $defSize;

global $input;
global $result;
global $stats;
global $gunPool;
global $accuracy;
global $shipID;
global $attShipAlive;
global $attShipRemaining;
global $attShipEmped;
global $attShipStolen;
global $attLostBuffer;
global $attEmpedBuffer;
global $attStolenBuffer;
global $defShipAlive;
global $defShipRemaining;
global $defShipEmped;
global $defShipStolen;
global $defLostBuffer;
global $defEmpedBuffer;
global $defStolenBuffer;
global $targetShipAmounts;

 

As I am convinced I can create a very simple OO design out of these.

Link to comment
Share on other sites

MacRedrum: do you have the global variables defined anywhere OUTSIDE of the functions?  Or are they being "created" by the function.  I have seen problems with a function creating a variable that has been defined as global (inside the function) not actually becoming global.

 

For Instance:

function doWhat() {
  global $something

  $something = pickOne();
}

 

But if you just declare the variable outside the function scope it seems to work

$something = ""; // This variable is global because it is outside the function scope

function doWhat() {
  global $something

  $something = pickOne();
}

 

It does not make sense to have to do it, and maybe I was fighting a different problem at the time, but it is worth a try.

 

If you get this working with all of these globals, you are going to want to review it and design a better approach, whether it is object oriented or not.  In fact, you may spend more time getting this to work as you have it than if you stop right now and redesign.

 

And before I get a bunch of hate mail: yes, there may be a better way to do this without having all of those global variables.  But PHP is loaded with globals and we ALL use them ALL the time; $_SERVER[] comes to mind, and oh yeah, $_SESSIONS, and maybe a few others? There are times when a global is the right solution (IMO).

 

Link to comment
Share on other sites

I do have functions that are repeatative too, but since the 1st step of the troubleshooting are the sort-functions, I mentioned them. The game has multiple types of ships that shoot with the same sort of formula, and thus I made a function of them.  It took around 300 lines of the code when I did it. There how ever might be a way to make them work without globals variables.

 

All those globals in the main function are things like amount of shots left, arrays of ship statuses(there're alive, able to shoot, stolen, frozen) then also buffers for those arrays, since there's a order in which they shoot and the values can only be updated in the right slot. Otherwise the ship amounts are corrupted.

 

DavidAM: that might be the case with those sort-functions too as they create the variables from the input array. I've tried running them in seperate file and they don't seem to even greate any values to the new variables. Maybe I can get them to work with the other functions... I hope.

 

Of course, I've been doing this engine for a looong time. Staring at it nonstop has made me at times what I call "code blind".  I even made a testing code for the engine with a never ending while-loop :) Then wondered what's so wrong with the main code for a day. Maybe I'll admit defeat and de-function atleat the sort-functions. The others should be much more easier. Shame, the engine looked so good with collapsed functions :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.