• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Class variables

Hello

When I create a class at PHP4, I create var $ip, ... under class. Under constructor, I write $this->ip = $_SERVER['REMOTE_ADDR'];. When I erase var $ip under class, it still works. What's the point of writing var $ip then?

Kind regards
0
hasozduru
Asked:
hasozduru
  • 3
  • 2
  • 2
2 Solutions
 
peyoxCommented:
Variables declared with var statement are called properties.They can be defined anywhere within the class, you should really define them at the very top, so you can better see the class’ properties. As a plus - you can initialize them in var statment.

And I think this is all about.
0
 
hasozduruAuthor Commented:
Yes but as I said, if I don't use var $ip, nothing changes. So what's the point of using var $ip at class?

Thanks
0
 
peyoxCommented:
Classes in PHP4 are not as good as they should be. They are very, very simple. VAR keyword may be omitted, however for your convenience and clarification you should use VAR keyword.

In PHP5 classes are similar to java classes (very powerful and fast). In PHP5 you can still use VAR keyword, however it will by default assign public scope to variables defined this way. Keywords public/private/protected introduced in version 5 should be used instead of VAR (as far as I know VAR keyword in PHP5 will generate E_STRICT warning in PHP5)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
_Marcel_Commented:
Indeed you are not obliged to declare/initialise a property under PHP. It is just like a normal variable; they may be declared beforehand, but when you don't do this, they get declared on the fly. So:

var $x;
$x = 0;

is the same as doing:

$x = 0;

There is no need to declare everything beforehand, but it may help you clarify your code. On the other hand, PHP is a (very) flexible language, the classes are not very rigid. You can always change the properties later. You can add them, like you already noticed, but with unset() you can also delete them. You can even add properties from outside the class:

class MyClass {
}
$myclass = new MyClass();
$myclass->myvariable = 1;

At that point the instance $myclass has an new property called myvariable. Notice that this is only true for that instance!

But to get back to your question, the only reason to use it is to make the code 'cleaner'. It is also possible to read these class-variables from the definition. All other variables are created on the fly, and are only valid for an instance. But since classes are not very developed in PHP4 you will not get into any trouble with interfaces etc. The again, when you use a function like get_class_vars() you will not get the variables that are defined on the fly.
0
 
hasozduruAuthor Commented:
Marcel

What does fly mean?

Thanks
0
 
peyoxCommented:
On the fly = "created when needed", created when used for the firs time.
0
 
_Marcel_Commented:
Thanx peyox, for answering that one.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now