Posted by Paul on 05/26/05 22:13
Bonge Boo! wrote:
> On 26/5/05 7:53 am, in article Gnele.1486038$8l.366880@pd7tw1no, "Paul"
> <freelance@dezignage.net> wrote:
>
>
>>sorry just a quick note I caught myself on...
>>
>>inside the object you'd have to declare the variable... like so..
>>
>>class Cart {
>> var $var;
>>
>> function Cart() {
>> $this->var =& $GLOBALS['var'];
>> }
>>
>>}
>
>
>
> Ok. Been playing with all this to try to get it too work. Normally with a
> function I could do the following:
>
> $foo = "apples";
>
> function myfunction () {
> extract($GLOBALS);
> $var = $foo;
> print $var;
> }
>
> myfunction();
>
> Outputs apples. Which is fine and tells me that extract($GLOBALS) puts all
> variables I have defined available in the function. If I try to use this
>
> extract($GLOBALS);
>
> Inside my class definition I get the following error.
>
> Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or
> `T_VAR' or `'}'' in /path/shoppingcart.inc.php on line 14
>
> So I guess for some reason I can't use extract GLOBALS in a class.
>
> When I try to use a defined constant when setting up my class variables then
> it doesn't seem to stick. I would have thought the below should work.
>
> var $password = SQL_PASSWORD;
>
> But nothing seems to be assigned to $password.
>
> If I try
>
> var $password = $GLOBALS['password'];
> or
> var $password =& $GLOBALS['password'];
>
> I get Parse error.
>
> So it seems like the $GLOBAL keyword is what is causing the problem when
> called inside a class. Does that make sense? It can't be that I am using
> reserved keywords can it?
>
I think the reason you're getting a parser error is nothing to do with
globals... you're giving it a value in the wrong place... it should be
like this...
class someClass {
var $password;
function someClass() { (or __Construct() in PHP 5)
$this->password = $GLOBALS['password'];
}
function someOtherFunctionInsideTheClass() {
$password =& $this->password;
// or you could probably even do this..
extract($GLOBALS);
}
}
but if you need it in every function of your class, I would just add a
reference in the beginnign of each function.. and that's it.. (that
shouldn't take that long...
function anotherFunctionOfTheClass ($arg, $arg2) {
$password =& $this->password;
}
maybe this will help? :)
[Back to original message]
|