/    Sign up×
Community /Pin to ProfileBookmark

Declaring variables in classes

Hi,

I am still new to PHP so have many queries in my head.

Why and/or when should you declare variables at the top of a class, such as this?:

var $mResult;
var $mUsername;
var $mPassword;

Variables appear to work fine, even when they are not declared in this way.

Thanks,
TK

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chazzyFeb 25.2008 — Do you mean something like this?

[code=php]
class MyClass
{
var $var1;
var $var2;
var $var3;

function myFunction1()
{
$anotherVar = 0;
}
}
[/code]


The difference is that $var1/2/3 represent "members" of the class - they exist as long as an instance of the class is created. you'll hear them called instance variables as well.

$anotherVar is different, it only exists inside the method.

When to use one or the other is dependent on the scope you want to use.
Copy linkTweet thisAlerts:
@AnotherMuggleauthorFeb 25.2008 — Do you mean something like this?

[code=php]
class MyClass
{
var $var1;
var $var2;
var $var3;

function myFunction1()
{
$anotherVar = 0;
}
}
[/code]


The difference is that $var1/2/3 represent "members" of the class - they exist as long as an instance of the class is created. you'll hear them called instance variables as well.

$anotherVar is different, it only exists inside the method.

When to use one or the other is dependent on the scope you want to use.[/QUOTE]


Just the kind of answer I was after.

Thanks a lot ?
Copy linkTweet thisAlerts:
@ZnupiFeb 25.2008 — I only declare variables at the top of the class when I need them to have a default value, like this:
[code=php]
class Something {
var $myVar = "default value";
}
[/code]

Otherwise, it's perfectly safe to do:
[code=php]
class Something {
function myFunc() {
$this->myVar = "some value";
}
}
[/code]

?
Copy linkTweet thisAlerts:
@TJ111Feb 25.2008 — I declare all of the ones that are predefined at the top, or if I need to set the scope (or both). Keep in mind if you are using PHP5 you should use 'public/private/protected' instead, as 'var' is PHP4 syntax and might not be supported in PHP versions. Also I list out any variables that are created in the __construct function. Its good practice to be write out all the variables you use at the top, its like a quick cheat sheet for the class.
[code=php]
Class MyClass
{
public $var1 = 18;
protected $var2 = "19";
private $var3 = "private";

public $name;
private $id;

function __construct($name, $id) {
$this->name = $name;
$this->id = $id;
}
}[/code]
×

Success!

Help @AnotherMuggle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...