0

-EDIT- Yes this actually does work. I see that now...

Is it possible to use a variable to determine a property?

I have 2 classes that are called as part of my controller

$this->document->setPageNum

and

$this->document2->setPageNum

I would like to use something like

if (is_array($pagenum)) {
    $doc = 'document';
} else {
    $doc = 'document2';
}

$this->$doc->setPageNum = $pagenum;

Is that possible to do?

3
  • 3
    wouldn't it be a fun experience to just try it out? o.o Commented May 31, 2011 at 4:17
  • ah. damn.. I did try and didn't work. Didn't bother to realize I had typo. It does work exactly like I hoped. Thanks! Commented May 31, 2011 at 4:20
  • 1
    Indeed, it does work. ;-) Commented May 31, 2011 at 4:29

1 Answer 1

3

why not save yourself the trouble of confusing code and just set the variable equal to the actual object you want, like so:

if (is_array($pagenum)) {
    $doc = $this->document;
} else {
    $doc = $this->document2;
}

$doc->setPageNum = $pagenum;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.