noidtluom Posted September 8, 2007 Share Posted September 8, 2007 Just been using this :: recently and came across this problem and unable to fix it. NOTE: I am using PHP 4. <?php class Dog { // constructor function Dog() { // BEGIN constructor static $myString = 'Seriously totally awesome'; } // END constructor } // END class Dog echo Dog::$myString; ?> The "echo Dog::$myString doesn't work. I am getting this error: Parse error: syntax error, unexpected ';', expecting '(' The line with the error is the one that contains "echo Dog::$myString;". Just as an extra note, I can do Dog::myFunction(); without any errors. Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/ Share on other sites More sharing options...
benjaminbeazy Posted September 8, 2007 Share Posted September 8, 2007 try this class Dog { function Dog() { $this->myString = 'Seriously totally awesome'; } } $dog = new Dog(); echo $dog->myString; Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344270 Share on other sites More sharing options...
benjaminbeazy Posted September 8, 2007 Share Posted September 8, 2007 as i understand it, the reason you can't call the variable as you are doing is because it isn't defined until the object is instantiated and the constructor is called. if the static variable definition was outside the constructor, it would work. assuming correct syntax. Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344274 Share on other sites More sharing options...
noidtluom Posted September 9, 2007 Author Share Posted September 9, 2007 as i understand it, the reason you can't call the variable as you are doing is because it isn't defined until the object is instantiated and the constructor is called. if the static variable definition was outside the constructor, it would work. assuming correct syntax. You might have misunderstood me. That is exactly what I am trying to escape. I am trying to call a variable or function in the class without creating a new instance. I thought that the "::" operator could do that? Oh, and in PHP 4 it has to be defined in the constructor I think. Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344501 Share on other sites More sharing options...
matthewhaworth Posted September 9, 2007 Share Posted September 9, 2007 The problem you're having is that the static variable is private to that function and therefore cannot be called externally. If you made it look like this.. <?php class Dog { public var $myString; // constructor function DogConstructor() { // BEGIN constructor $this->myString = 'Seriously totally awesome'; } // END constructor } // END class Dog echo Dog::DogConstructor; echo Dog::$myString; ?> This might not work in PHP4 though.. Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344511 Share on other sites More sharing options...
noidtluom Posted September 9, 2007 Author Share Posted September 9, 2007 Nope, still not working. By the way, I don't think you can do "echo Dog::DogConstructor;". It should be "Dog::DogConstructor();". Also, in PHP 4 the constructor is basically function nameOfMyClass, so function DogConstructor wouldn't technically be the constructor. Can anybody do a simple example using :: for both a function and a variable that works in PHP 4? I will really appreciate that. That should also solve my problem Thanks in advance. Awaiting your useful replies! Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344658 Share on other sites More sharing options...
wildteen88 Posted September 9, 2007 Share Posted September 9, 2007 A good example is always the PHP manual. Can't go wrong there. Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344665 Share on other sites More sharing options...
noidtluom Posted September 9, 2007 Author Share Posted September 9, 2007 Hmm I was viewing a different scope page. I'm now pretty confused because in the example it shows this: <?php class Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } ?> As you can see $variables are used there perfectly fine. As a note, in PHP 4 you cannot define as public. For compatibility with PHP 4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public. So I'm quite confused how to actually define a static or const variable to start with. For reference you can read the note on that page which I found useful and slightly contradictory: According to the documentation, there is no way to have a class variable, only class functions. There should be class variables, because there's no way to implement CONSTANTS. Something like this won't work: <? class Employee { var $ACTIVE_EMPLOYEE = 1; //etc.. } echo Employee::ACTIVE_EMPLOYEE; //Also tryed: echo Employee::$ACTIVE_EMPLOYEE ?> returns: Parse error: parse error, unexpected ';', expecting '(' in /home/x/public_html/Test.php on line 9 It forces you to create an instance of the class: $emp = new Employee(); echo $emp->ACTIVE_EMPLOYEE; Any help? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344672 Share on other sites More sharing options...
noidtluom Posted September 9, 2007 Author Share Posted September 9, 2007 OK my conclusion is that calling a variable with it can only be done with PHP 5. Since I want my application to be PHP 4 compatible I guess I will have to stick to this: <?php function example() { echo "I am the original function A::example().<br />\n"; } ?> Then just call the Class::example(); Oh well. Solved. (at least) Quote Link to comment https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/#findComment-344727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.