necesito función que va a hacer algo como esto:PHP con el parámetro constructor
$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;
cuando escribo algo como esto:
$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
No sé cómo hacer que el constructor parametrizado en PHP.
Ahora uso constructor de la siguiente manera:
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;
}
}
Tienes razón. No funciona. ¿Cómo puedo inicializar la propiedad {'fvalue-string'} con el valor predeterminado? – Kamil
Probé el constructor con múltiples parámetros ayer y no funcionó, estaba haciendo algo mal, porque tengo errores. Vi muchos ejemplos de __construct (array) y "desempaquetar" array dentro del constructor, así que pensé que no puedo hacer constructor con muchos parámetros en PHP. Estaba equivocado. Gracias. – Kamil