Re: Anyway to access a class variable without using a return function?
| From: | Tom Rogers | Date: | Mon, 15 Mar 2004 02:57:02 +0000 |
| Subject: | Re: Anyway to access a class variable without using a return function? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-180410@lists.php.net to get a copy of this message | ||
Hi,
Monday, March 8, 2004, 3:42:30 PM, you wrote:
T> Hi List,
T> I have a class with a constructor that sets the variables and I currently
T> use functions to return each one. When using alot of variables in the
T> constructor i tend to have many return functions. Is there a way to access
T> the constructor variables without using a return function?
T> This is how I do it:
T> class MyClass {
T> function MyClass {
T> $this->var1="1";
T> $this->var2="2";
T> }
T> function GetVar1() {
T> return $this->var1;
T> }
T> function GetVar2() {
T> return $this->var2;
T> }
T> }
T> Thanks
Should be something like this:
class MyClass {
var $var1;
vat $var2;
function MyClass {
$this->var1="1";
$this->var2="2";
}
function GetVar1() {
return $this->var1;
}
function GetVar2() {
return $this->var2;
}
}
$test = new MyClass();
echo $test->var1.'<br>';
echo $test->var2.'<br>';
This is not recommended by OOP buffs but it is perfectly legal.
--
regards,
Tom