Dss
Dss

Reputation: 2360

PHP. Possible to use variable for object?

-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?

Upvotes: 0

Views: 448

Answers (1)

nathan gonzalez
nathan gonzalez

Reputation: 11987

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;

Upvotes: 3

Related Questions