PHP: Come creare un'istanza di una classe con argomentazioni dall'interno di un'altra classe
-
13-09-2019 - |
Domanda
Sono in una situazione in cui ho bisogno per creare un'istanza di una classe con argomenti all'interno di un'istanza di un'altra classe. Ecco il prototipo:
//test.php
class test
{
function __construct($a, $b, $c)
{
echo $a . '<br />';
echo $b . '<br />';
echo $c . '<br />';
}
}
Ora, ho bisogno di creare un'istanza di sopra classe utilizzando al di sotto della classe CLS Funzione:
class myclass
{
function cls($file_name, $args = array())
{
include $file_name . ".php";
if (isset($args))
{
// this is where the problem might be, i need to pass as many arguments as test class has.
$class_instance = new $file_name($args);
}
else
{
$class_instance = new $file_name();
}
return $class_instance;
}
}
Ora, quando cerco di creare un'istanza della classe di test, mentre il passaggio di argomenti ad esso:
$myclass = new myclass;
$test = $myclass->cls('test', array('a1', 'b2', 'c3'));
Si dà errore: Manca argomento 1 e 2; solo il primo argomento è passato.
Questo funziona bene se ho un'istanza di una classe che non ha argomenti in esso è funzione di costruzione.
Per gli sviluppatori PHP esperti, soprattutto non dovrebbe essere un gran problema. Si prega di aiutare.
Grazie
Soluzione
http://php.net/manual/en/class.reflectionclass .php
if(count($args) == 0)
$obj = new $className;
else {
$r = new ReflectionClass($className);
$obj = $r->newInstanceArgs($args);
}
Altri suggerimenti
È possibile:
1) Modificare classe di test di accettare una matrice, che contiene i dati che si desidera passare.
//test.php
class test
{
function __construct($a)
{
echo $a[0] . '<br />';
echo $a[1] . '<br />';
echo $a[2] . '<br />';
}
}
2) avviare utilizzando un metodo utente anziché il costruttore e chiamare utilizzando la funzione call_user_func_array()
.
//test.php
class test
{
function __construct()
{
}
public function init($a, $b, $c){
echo $a . '<br />';
echo $b . '<br />';
echo $c . '<br />';
}
}
Nella classe principale:
class myclass
{
function cls($file_name, $args = array())
{
include $file_name . ".php";
if (isset($args))
{
// this is where the problem might be, i need to pass as many arguments as test class has.
$class_instance = new $file_name($args);
call_user_func_array(array($class_instance,'init'), $args);
}
else
{
$class_instance = new $file_name();
}
return $class_instance;
}
}
http://www.php.net /manual/en/function.call-user-func-array.php
Infine, è possibile lasciare il costruttore PARAMS vuoto e utilizzare func_get_args()
.
//test.php
class test
{
function __construct()
{
$a = func_get_args();
echo $a[0] . '<br />';
echo $a[1] . '<br />';
echo $a[2] . '<br />';
}
}
Si potrebbe utilizzare call_user_func_array () I credere.
o si potrebbe lasciare la lista argomenti del costruttore, e poi dentro l'utilizzo costruttore di questo
$args = func_get_args();
class textProperty
{
public $start;
public $end;
function textProperty($start, $end)
{
$this->start = $start;
$this->end = $end;
}
}
$ oggetto = new textProperty ($ inizio, $ end);
non funzionano?
Il modo più semplice che ho trovato:
if ($depCount === 0) {
$instance = new $clazz();
} elseif ($depCount === 1) {
$instance = new $clazz($depInstances[0]);
} elseif ($depCount === 2) {
$instance = new $clazz($depInstances[0], $depInstances[1]);
} elseif ($depCount === 3) {
$instance = new $clazz($depInstances[0], $depInstances[1], $depInstances[2]);
}
Ci dispiace un po 'crudo, ma è necessario comprendere l'idea.
Siamo nel 2019 la società e abbiamo php7 ora ... e noi abbiamo la diffusione operatore (...). Ora possiamo semplicemente chiamare
<?php
class myclass
{
function cls($file_name, $args = array())
{
include $file_name . ".php";
if (isset($args))
{
$class_instance = new $file_name(...$args); // <-- notice the spread operator
}
else
{
$class_instance = new $file_name();
}
return $class_instance;
}
}