php字符串对象,PHP字符串到对象名称
好的我有一個字符串……
$a_string = "Product";
我想在調用這樣的對象時使用這個字符串:
$this->$a_string->some_function();
狄更斯如何動態調用該對象?
(不要以為我在PHP 5心中)
解決方法:
所以你要使用的代碼是:
$a_string = "Product";
$this->$a_string->some_function();
這段代碼暗示了一些事情.一個名為Product的類,其方法為some_function(). $this具有特殊含義,僅在類定義中有效.所以另一個類將擁有Product類的成員.
因此,為了使您的代碼合法,這是代碼.
class Product {
public function some_function() {
print "I just printed Product->some_function()!";
}
}
class AnotherClass {
public $Product;
function __construct() {
$this->Product = new Product();
}
public function callSomeCode() {
// Here's your code!
$a_string = "Product";
$this->$a_string->some_function();
}
}
然后你可以用這個來調用它:
$MyInstanceOfAnotherClass = new AnotherClass();
$MyInstanceOfAnotherClass->callSomeCode();
標簽:php,oop,string
來源: https://codeday.me/bug/20190627/1300076.html
總結
以上是生活随笔為你收集整理的php字符串对象,PHP字符串到对象名称的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 峰米T1怎么登陆腾讯会员
- 下一篇: php 字符串匹配 like,Think