Portál AbcLinuxu, 26. prosince 2025 11:12
class Kontrola
{
public function __construct($array)
{
return 'foo';
}
public function TestOne($a, $b, $c) //Potřebné 3 parametry
{
return 'foo';
}
public function TestTwo() //Bez parametru
{
return 'foo';
}
public function TestTre($a) //Potřebný 1 parametr
{
}
}
$array=array('TestOne'=>'0','0','0',
'TestOne'=>'0','2','0',
'TestTre'=>'2',
'TestTwo',
'TestOne'=>'2','3','10',
);
$Kontrola = New Kontrola($array);
echo $Kontrola;
Jak mám tedy v konstruktoru volat jednotlivé metody s příslušnými parametry, když každá metoda má jiný počet parametrů. Poradí prosím někdo? Děkuji
Řešení dotazu:
Díky
jako vy chcete neco takoveho?
$array=array('TestOne'=>array('0','0','0'),
'TestOne'=>array('0','2','0'),
'TestTre'=>array('2'),
'TestTwo'=>array(),
'TestOne'=>array('2','3','10'),
);
public function __construct($array)
{
foreach($array as $key=>$params){
var_dump(call_user_func_array('parent::'.$key,$params));
}
}
array($this, 'parent::'.$key)'Class::method' je pro metody třídy (a funguje v poměrně novém PHP); varianta array($object, 'method') je pro normální metody objektu. Tazatel spíš bude chtít to druhé, ale těžko říci...
tak tady mate jeden nazev funkce, dva pravdepodobne funkcni kody, pro jistotu pridam i odkaz do dokumentace - http://php.net/manual/en/function.call-user-func-array.php
vite urcite, co chcete?
<?php
class Kontrola
{
public function __construct($array)
{
foreach($array as $key => $value){
call_user_func_array(array(&$this, $key) , $value );
}
}
public function TestOne($a, $b, $c) //Potřebné 3 parametry
{
echo "TestOne 1: $a 2: $b 3: $c\n";
return 'foo';
}
public function TestTwo() //Bez parametru
{
echo "TestTwo\n";
return 'foo';
}
public function TestTre($a) //Potřebný 1 parametr
{
echo "TestTre 1: $a\n";
return 'bar';
}
}
$array=array('TestOne'=>array('0','0','0'),
'TestOne'=>array('0','2','0'),
'TestTre'=>array('2'),
'TestTwo'=>array(),
'TestOne'=>array('2','3','10')
);
$Kontrola = New Kontrola($array);
no tak treba takhle... zaklady php :)
class Kontrola
{
public function __construct($array)
{
foreach($array as $map){
call_user_func_array(array(&$this, $map[0]) , $map[1] );
}
}
public function TestOne($a, $b, $c) //Potřebné 3 parametry
{
echo "TestOne 1: $a 2: $b 3: $c\n";
return 'foo';
}
public function TestTwo() //Bez parametru
{
echo "TestTwo\n";
return 'foo';
}
public function TestTre($a) //Potřebný 1 parametr
{
echo "TestTre 1: $a\n";
return 'bar';
}
}
$array=array(array('TestOne',array('0','0','0')),
array('TestOne',array('0','2','0')),
array('TestTre',array('2')),
array('TestTwo',array()),
array('TestOne',array('2','3','10'))
);
$Kontrola = New Kontrola($array);
$array=array('TestOne'=>array('0','0','0'),
'TestOne'=>array('0','2','0'),
'TestTre'=>array('2'),
'TestTwo'=>array(),
'TestOne'=>array('2','3','10')sa napr. použije$array=array(('TestOne', '0','0','0'),
('TestOne', '0','2','0'),
('TestTre', '2'),
('TestTwo'),
('TestOne', '2','3','10')(a náležite sa upraví ten kód)
):
<?php
class Kontrola
{
public function __construct($array)
{
foreach($array as $rec){
call_user_func_array(array(&$this, $rec[0]) , $rec[1] );
}
}
public function TestOne($a, $b, $c) //Potřebné 3 parametry
{
echo "TestOne 1: $a 2: $b 3: $c\n";
return 'foo';
}
public function TestTwo() //Bez parametru
{
echo "TestTwo\n";
return 'foo';
}
public function TestTre($a) //Potřebný 1 parametr
{
echo "TestTre 1: $a\n";
return 'bar';
}
}
$array=array(array('TestOne',array('0','0','0')),
array('TestOne',array('0','2','0')),
array('TestTre',array('2')),
array('TestTwo',array()),
array('TestOne',array('2','3','10'))
);
$Kontrola = New Kontrola($array);
TestOne 0 0 0 TestTre 2 TestTwo TestOne 2 3 10který se načte a ověří, že zavolání metod vyprodukuje očekávané výsledky. Na unit test je vhodný nějaký framework, ale coby zadávání test cases pro jednoduché funkce to není tak špatné řešení -- lepší než pro ně patlat kód. Prasečina je totiž representovat data kódem...
taky to muze pouzit na nejake dynamicke nacitani modulu a extra funkce nejake podstranky... treba na urcitem miste na strance bude priznak s nazvem tridy a metody a tim to pak zavolat
bezpecnost samozrejme neresim :)
class Kontrola
{
public function __construct($array)
{
foreach($array as $test) {
$reflectionMethod = new ReflectionMethod($this, $test[0]);
echo $reflectionMethod->invokeArgs($this, $test[1]);
}
}
public function TestOne($a, $b, $c) //Potřebné 3 parametry
{
return 'one';
}
public function TestTwo() //Bez parametru
{
return 'two';
}
public function TestTre($a) //Potřebný 1 parametr
{
return "three";
}
}
$array = [
['TestOne', ['0','0','0']],
['TestOne', ['0','2','0']],
['TestTre', ['2']],
['TestTwo', []],
['TestOne', ['2','3','10']]
];
new Kontrola($array);
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.