I think you could also do something like this:

class MyClass {
     function MyClass {
         $this->var1="1";
         $this->var2="2";
     }
     function GetVar($var) {
         return $this->{$var};
     } 
}

$test = new MyClass();

echo $test->GetVar('var1').'<br />';
echo $test->GetVar('var2').'<br />';

-----Original Message-----
From: Tom Rogers [mailto:[EMAIL PROTECTED] 
Sent: 15 March 2004 02:57
To: Terence
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Anyway to access a class variable without using a return
function?

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 
T> currently use functions to return each one. When using alot of 
T> variables in the constructor i tend to have many return functions. Is 
T> there a way to access 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

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to