tags:

views:

72

answers:

3

hello now i have class

class test{
var $var_test = 'test';

}

how i can get it

i think

$t = new test();
echo $t->var_test;

is this true

+7  A: 

Have you tried it?

But yes, that is true. (It would be better to declare the member with public if you're going to access it from outside the class.)

Skilldrick
yes its workedthanks
moustafa
Good. Just remember, declare members with `private` wherever possible, only with `public` if they need to be accessed from outside.
Skilldrick
+2  A: 

If your variable is public, yes. Be sure to use proper visibility for your variables and methods.

Typically, you'll want to use get() and set() methods to handle data within the class itself. These keeps people's grubby hands off of your data :) Generally these will return the value from within the class ( return $this->val; ) so nobody is directly able to access the variable.

Jonathan Sampson
visibility isn't just about keep other people's hands off your data - it's for your own grubby hands, too! by declaring something as private, you know with 100% certainty that neither you nor anyone else is using it in some other code, so you're free to alter it or remove it in the future without having to trawl through your entire project cleaning up all the places where someone was too lazy to use proper accessors.
nickf
+1  A: 

If you are wondering if

$t = new test();
echo $t->var_test;

is correct, then the answer is yes. I guess that you made the question because you are having problems with code you developed, and you want to understand why it is not working. If that is the case, then you should report the exact code you are using.

As side note, the code you are wrote is for PHP4; PHP5 uses a different syntax for class declaration, even though it is able to parse PHP4 classes.

kiamlaluno