?
Solved

Class variables

Posted on 2005-04-03
7
Medium Priority
?
204 Views
Last Modified: 2006-11-18
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
Comment
Question by:hasozduru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:peyox
ID: 13694525
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
 

Author Comment

by:hasozduru
ID: 13694555
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
 
LVL 6

Assisted Solution

by:peyox
peyox earned 750 total points
ID: 13694634
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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 8

Accepted Solution

by:
_Marcel_ earned 750 total points
ID: 13696284
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
 

Author Comment

by:hasozduru
ID: 13697259
Marcel

What does fly mean?

Thanks
0
 
LVL 6

Expert Comment

by:peyox
ID: 13697550
On the fly = "created when needed", created when used for the firs time.
0
 
LVL 8

Expert Comment

by:_Marcel_
ID: 13699854
Thanx peyox, for answering that one.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question