Import a Variable from a Function (so that It Can Be Used in Another Place)

The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Now the thing is this all worked perfectly when I defined $total as being the global variable, but then I thought that it would be risky to have so important a variable have so generic a name (as it might clash with another function somewhere) so I'm trying to commit it's value into the global variable called $grandTotal.

This doesn't seem to be happening, though.

I also tried...

Code PHP:

// Export Global Variables$GLOBALS['grandTotal']=$total;

...with no luck.

Suggestions??

Trying to fill the unforgiving minute
with sixty seconds' worth of distance run.

I've had a similar problem to Shaun and have read about the evils of globals, though more to do with Javascript. Still, I guess the same basic principle applies.

If I do what you've done there, cranial-bore, does that mean all functions have to be in classes? Because if I want to access anything in $priceObject within a function in the global scope, I'd still have to do global $priceObject, or at least that's what it seems like to me.

PHP Code:

<?phpclass pricing{ public $total; public $grand_total; public $count;

I suppose the clear solution to that is to put discount() inside pricing. Does that mean functions in the global scope are also a bad idea? Also, does $priceObject not count as a "global variable" because it's an object? It's still in the global scope.

What you're doing, though, is mixing OO with procedural programing. Don't. If you're going OO, every function (i.e. method) should be a member of a particular class.

In your example, discount() most likely should be a member of pricing class. If not, then some other class, in which case $priceObject would be passed to the discount() method (having both $priceObject and $discountObject in the same scope, of course)

For the vast majority of cases it's preferable to pass and return rather than access globals.
You can pass one object to another, or instantiate an object inside another.

Discounting should probably be a method of the pricing object, but if it were complex enough (ie. needed multiple methods) it could be its own class.
Remember that having paramaters/arguments in a class constructor means that the class can't be instantiated without them.
This method can be used to avoid globals. You set the required object as an argument of the object constructor and then the calling code will error out if its not passed when creating the object.

Consider:

PHP Code:

<?phpclass pricing{// .... (as before)}

class discount{ private $priceObj; //private member object for use INSIDE this class