Page 1 of 1

$$ for classes variables?

Posted: Thu Jan 27, 2011 12:24 pm
by jaceinla
Hi all, I'm aware that:

Code: Select all

$dog = "cat";
$cat = "dog";

echo $$dog; will echo out "dog"
Can this be done with class variables?

Code: Select all

protected cat;
protected dog;

public function __construct() {
    $this->cat = "dog";
    $this-dog = "cat";
}

public function echoOut() {
    $this->this->dog;    //does not work
}

Why am I asking? I'm getting an image height and width, I need to find the bigger of the two, so I have $this->bigger and $this->smaller which either equal "height" or "width" so that when I use these variables, I'm using them as follows:

Code: Select all

$this->ratio = $this->bigger/$this->smaller;   //bigger and smaller are strings that refer to already set variables $this->height, $this->width
Thanks in advanced!

PS. I know I can do an if statement to get the ratio once and keep going, but this option is a little cleaner and flexible.

Re: $$ for classes variables?

Posted: Thu Jan 27, 2011 12:51 pm
by Weirdan

Code: Select all

echo $this->{$this->bigger};

Re: $$ for classes variables?

Posted: Thu Jan 27, 2011 1:23 pm
by jaceinla
Thank you!