Page 1 of 1

What the HECK!? Problems w/ simple class & I tried everthing

Posted: Fri May 22, 2009 8:34 pm
by omniuni
Note: Title should read: What the HECK!? Problems with the simplest of class, and I've tried everything I know!!!

Sorry for beginning "What the HECK!?" but this is driving me absolutely insane.

:banghead:

OK, this one is driving me INSANE. I SWEAR there are other files on this very server written in ways this is constantly failing.

First of all, I am using PHP Version 5.2.6-3

Here is some code that works:

Code: Select all

class ipstuff{
    var $userIP = '123';
}
$teststuff = new ipstuff;
echo $teststuff->userIP;
But this does not:

Code: Select all

 
class ipstuff{
    $userIP = '123';
}
$teststuff = new ipstuff;
echo $teststuff->userIP;
I am told:
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/daniel/public_html/testing/se/se/index.php on line 8

and this does not work either:

Code: Select all

class ipstuff{
    var $userIP = $_SERVER['remote_addr'];
}
$teststuff = new ipstuff;
echo $teststuff->userIP;
I am told:
Parse error: syntax error, unexpected T_VARIABLE in /home/daniel/public_html/testing/se/se/index.php on line 8

Nor does this work:

Code: Select all

class ipstuff{
    var $userIP = '123';
}
echo ipstuff::$userIP;
Now, that I understand, it needs to be a static var. So I try:

Code: Select all

class ipstuff{
    static var $userIP = '123';
}
echo ipstuff::$userIP;
and am told:
Parse error: syntax error, unexpected T_VAR, expecting T_VARIABLE in /home/daniel/public_html/testing/se/se/index.php on line 8

which is OK, I guess, it needs a variable, not a var, so I'll try:

Code: Select all

class ipstuff{
    static $userIP = '123';
}
echo ipstuff::$userIP;
which does work, but

Code: Select all

class ipstuff{
    static $userIP = $_SERVER['REMOTE_ADDR'];
}
echo ipstuff::$userIP;
does not! I am told:
Parse error: syntax error, unexpected T_VARIABLE in /home/daniel/public_html/testing/se/se/index.php on line 8

yet if I take it out and just run:

Code: Select all

echo $_SERVER['REMOTE_ADDR'];
then I get my IP address!!!

Can SOMEONE PLEASE explain this insanity!!!! Why do I suddenly have to declare my variables in my classes, instead of just using them? Why will $_SERVER break a class that works fine for everything else? Why can't I use a "static var" and why does it do similar things if I try to even declare a "private" var!?

:banghead: I am so confused. :cry:

Re: What the HECK!? Problems w/ simple class & I tried everthing

Posted: Fri May 22, 2009 8:49 pm
by Scriptor
did ya try to globalize the variable?

global $_SERVER;

Re: What the HECK!? Problems w/ simple class & I tried everthing

Posted: Fri May 22, 2009 8:59 pm
by Christopher
Never use global (you don't need it for superglobals anyway) and don't use static until you actually know what you are doing. Just make plain classes. Probably the best way would be without usign a property at all because I assume you don't want to set the IP address. So:

Code: Select all

class Ipstuff{
    public function getUserIP() {
        return $_SERVER['REMOTE_ADDR'];
    }
}
If you really need to use a property for access then maybe something like this:

Code: Select all

class Ipstuff{
    public $userIP;
 
    public function __construct() {
        $this->userIP = $_SERVER['REMOTE_ADDR'];
    }
}

Re: What the HECK!? Problems w/ simple class & I tried everthing

Posted: Fri May 22, 2009 9:05 pm
by omniuni
Thanks... but...

Code: Select all

class ipstuff{
    static $userIP = global $_SERVER['REMOTE_A'];
}
echo ipstuff::$userIP;
gives me:
Parse error: syntax error, unexpected T_GLOBAL in /home/daniel/public_html/testing/se/se/index.php on line 8

same for:

Code: Select all

class ipstuff{
    var $userIP = global $_SERVER['REMOTE_ADDR'];
}
echo ipstuff::$userIP;
I'll try casting to a string:

Code: Select all

class ipstuff{
    static $userIP = (string)$_SERVER['REMOTE_ADDR'];
}
echo ipstuff::$userIP;
which gives me:
Parse error: syntax error, unexpected T_STRING_CAST in /home/daniel/public_html/testing/se/se/index.php on line 8

so let me try casting to a string first:

Code: Select all

class ipstuff{
    $anIP = (string)$_SERVER['REMOTE_ADDR'];
    static $userIP = $anIP;
}
echo ipstuff::$userIP;
results in:
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/daniel/public_html/testing/se/se/index.php on line 8

WHAT!?!? OK, how about let's forget the class:

Code: Select all

$anIP = (string)$_SERVER['REMOTE_ADDR'];
$userIP = $anIP;
 
echo $userIP;
Now THAT works fine!!!!

I am absolutely and completely baffled.

Now, continuing:

Code: Select all

class Ipstuff{
    public $userIP;
 
    public function __construct() {
        $this->userIP = $_SERVER['REMOTE_ADDR'];
    }
}
 
$testme = new Ipstuff;
echo $testme->userIP;
does indeed work.

Can someone though PLEASE explain why I can not do this the other ways I have tried???

EDIT: never mind. I guess I'm just going insane. :crazy: I have always declared public or private before, due to old Java habits. I just never realized it.

THANK YOU EVERYONE!

Re: What the HECK!? Problems w/ simple class & I tried everthing

Posted: Fri May 22, 2009 9:38 pm
by Christopher
omniuni wrote:Can someone though PLEASE explain why I can not do this the other ways I have tried???
You are doing a lot of strange stuff there. Stop with the global and static. It is not working because it is just way off. See the manual or build from the examples I gave you.

Re: What the HECK!? Problems w/ simple class & I tried everthing

Posted: Sat May 23, 2009 12:22 am
by omniuni
yeah, I noticed that.

It was driving me nuts for a while, but I figured it out eventually. See the last two sentences of my last post.

Believe it or not, these are not by far my first classes. I found out about the specifics of :: today, and I wanted to try to use it to make my life easier.

Well, that thought pretty much died.

Thanks, though.

Still, do you have any idea why trying to use $_SERVER not in a function in a class is completely failing?

I don't see the difference between

$a = 'hello';
and $a = $_SERVER['something'];

Re: What the HECK!? Problems w/ simple class & I tried everthing

Posted: Sat May 23, 2009 4:47 am
by Weirdan
omniuni wrote:Still, do you have any idea why trying to use $_SERVER not in a function in a class is completely failing?
Initializers have to be constant values (known at compile time). Thus the following works:

Code: Select all

class A {
   var $q = 'sdfg';
}
But this does not:

Code: Select all

$e = 'sdfg';
class A {
   var $q = $e;
}

Re: What the HECK!? Problems w/ simple class & I tried everthing

Posted: Sat May 23, 2009 8:15 pm
by omniuni
Ah! Thank you Weirdan! That makes sense! I always forget that most servers do indeed "compile" the PHP at runtime. I guess everything directly in a class must be described as something constant. You an modify, therefore, with setters and getters, hence why they're so prevalent, and why they drilled them so into us in Java class. I was just going bat-doodoo insane trying to figure this out. See this guy?
:evil:
That was me last night.

Now, thanks to you all, not only is my new CMS looking like very pretty code, I understand WHY my code is so pretty. :D

Granted, some of that may be my new syntax highlighting color scheme I made, but it's more fun to credit you all.