Re: Anyway to access a class variable without using a return function?

From: Date: Mon, 15 Mar 2004 13:10:59 +0000
Subject: Re: Anyway to access a class variable without using a return function?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-180436@lists.php.net to get a copy of this message
I mean using get_object_vars() is much better. and in the example I've seen there: you HAVE TO define all the variables you're using in a class scope. eg class myClass { var $var1; var $var2 = array(); function myClass($var) { $vars =get_object_vars($this); if (isset($vars[$var])) return $vars[$var]; return false; } } /tom On Mon, 15 Mar 2004 13:36:07 +0100 Marco Schuler <listworks@gmx.ch> wrote: > Hi! > > Am Mo, 2004-03-15 um 13.02 schrieb Dave Starling: > > 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 />'; > > Better check first if property/var exists: > > function getVar($var) > { > $that = null; > if (isset($this->$var)) { > $that = $this->$var; > } > return $that; > } > > > -- > Regards > Marco > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php --

« previous php.general (#180436) next »