[英]how to set instance variable from inside of method and set variable name the same as its value
如何從方法內部設置實例變量並將變量名稱設置為其值? 說明以代碼形式-查看從1到4的步驟。
我知道我可以使用$$variable
但是如何使用新名稱使它成為實例變量?
class Control
{
//2. i pass the name of class and make a new object
function model($object)
{
// 3. I create here variable with the name i set. so now it should be
// accessible as $HomeModel but how to make it accessible for other methods.
$$object=new $object();
}
}
class HomeController extends Control
{
//THIS IS START
public function __construct()
{
// 1. when i set the name here ['HomeModel'] and run class method model
$this->model('HomeModel');
//4. and use it ie. here
$data=$this->HomeModel->getData();
}
}
class HomeModel
{
public function getData()
{
echo "data";
}
}
$x = new HomeController();
應該:
class Control
{
//2. i pass the name of class and make a new object
function model($object)
{
// 3. i want to create here an instance variable with the name i set
$this->{$object} = new $object();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.