开发者

PHP. Possible to use variable for object?

开发者 https://www.devze.com 2023-03-09 03:35 出处:网络
-EDIT- Yes this actually does work. I see that now... Is it possible to use a variable to determine a property?

-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 con开发者_开发百科troller

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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消