muszę funkcję, która zrobi coś takiego:PHP konstruktor z parametrem
$arr = array(); // this is array where im storing data
$f = new MyRecord(); // I have __constructor in class Field() that sets some default values
$f->{'fid'} = 1;
$f->{'fvalue-string'} = $_POST['data'];
$arr[] = $f;
$f = new Field();
$f->{'fid'} = 2;
$f->{'fvalue-int'} = $_POST['data2'];
$arr[] = $f;
gdy piszę coś takiego:
$f = new Field(1, 'fvalue-string', $_POST['data-string'], $arr);
$f = new Field(2, 'fvalue-int', $_POST['data-integer'], $arr);
// description of parameters that i want to use:
// 1 - always integer, unique (fid property of MyRecord class)
// 'fvalue-int' - name of field/property in MyRecord class where next parameter will go
// 3. Data for field specified in previous parameter
// 4. Array where should class go
nie wiem jak zrobić sparametryzowanego konstruktora w PHP.
Teraz używam konstruktora tak:
class MyRecord
{
function __construct() {
$default = new stdClass();
$default->{'fvalue-string'} = '';
$default->{'fvalue-int'} = 0;
$default->{'fvalue-float'} = 0;
$default->{'fvalue-image'} = ' ';
$default->{'fvalue-datetime'} = 0;
$default->{'fvalue-boolean'} = false;
$this = $default;
}
}
Masz rację. To nie działa. Jak mogę zainicjować właściwość {'fvalue-string'} z wartością domyślną? – Kamil
Próbowałem konstruktora z wieloma parametrami wczoraj i nie działało, robiłem coś nie tak, ponieważ mam błędy. Widziałem wiele przykładów tablicy __construct (array) i "unpacking" wewnątrz konstruktora, więc pomyślałem, że nie mogę utworzyć konstruktora z wieloma parametrami w PHP. Myliłem się. Dziękuję Ci. – Kamil