Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/68483-solved-quick-operator-question/
Share on other sites

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.

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.

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..

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!

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.

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)

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.