How to pass the value code to the constructor in the form of variables is as follows, because the constructor needs to input multiple values, and the number of values imported each time may not be fixed. if not, the default value in the construction of sweet potato is used, so here I use an array to pass in. below I have used implode to cut the array into strings and used "," connected, why after passing in & nbsp; all are transferred to the $ host variable. The result is & nbsp; ho how to pass the value to the constructor in the class in the form of a variable
The code is as follows, because the constructor needs to input multiple values, and the number of incoming values may not be fixed each time. if not, the default value in sweet potato is constructed, so here I use arrays for input. below I have used implode to cut the array into strings and used "," connected. why are the strings transferred to the $ host variable? the result is:
Host: 192.168.1.1 and 1212
Port: 80
That is to say, the post is passed to $ host, and the port is used by default in the class.
Class test {
Public function _ construct ($ host = "127.0.0.1", $ port = "80 ")
{
Echo 'host: '. $ host ."
";
Echo 'Port: '. $ port;
}
}
$ Data ['host'] = '192. 168.1.1 ';
$ Data ['port'] = '123 ';
$ Str = implode (',', $ data );
$ Obj = new test ($ str );
Result:
Host: 192.168.1.1 and 1212
Port: 80
Share:
------ Solution --------------------
$ Ref? =? New? ReflectionClass ('test ');
$ Obj? =? $ Ref-> newInstanceArgs ($ data );