Salve a tutti,
da qualche giorno più che in passato mi sto esercitando con OOP, e in attesa che di passare al 5 sto cercando di scrivere una classe che operi in in questo modo...
Prima di tutto, il codice che ho scritto:
Codice PHP:
<?php
class test {
var $_nome, $_cogn, $_maid, $_azid, $_flag;
// [Constructor function (php >=4)]
function test( $_nome, $_cogn, $_maid, $_azid, $_flag ) {
$this->_nome = $_nome;
$this->_cogn = $_cogn;
$this->_maid = $_maid;
$this->_azid = $_azid;
$this->_flag = intval( $_flag );
}
}
class testExtended {
var $_nome, $_cogn, $_maid, $_azid, $_flag, $_ext1, $_ext2, $_ext3;
// [Constructor function (php >=4)]
function testExtended( $_nome, $_cogn, $_maid, $_azid, $_flag, $_ext1, $_ext2, $_ext3 ) {
$this->test( $_nome, $_cogn, $_maid, $_azid, $_flag );
$this->_ext1 = $_ext1;
$this->_ext2 = $_ext2;
$this->_ext3 = $_ext3;
}
}
class testManage {
var $testContainer, $Categ;
function testManage( &$dbResource, $dbName ) {
// formo testContainer e accodo le categorie
$this->testContainer = array();
if( $this->dbName !== '' ) {
mysql_select_db( $this->dbName );
$ris = &mysql_query( '_____________QUERY_____________;' );
if( mysql_num_rows( $ris ) > 0 ) {
while( $row = &mysql_fetch_row( $ris ) ) {
$this->testContainer[] = $row[0] = array();
}
$this->testContainer['_inserted'] = array();
}
}
}
function setTest(){
// codice ??
}
}
?>
Io ho una serie di oggetti test, poi ho altri oggetti testExtended con proprietà aggiuntive.
Poi ho la classe testManage che tramite la funzione setTest() dovrebbe verificare quale dei due oggetti creare e in quale categoria inseirlo.
In pratica, vorrei che il risultato finale fosse un array del tipo:
Codice PHP:
testContainer = array(
[0] => 'categoria 1' = array(
['nome 1'] = testExtended Object,
['nome 2'] = testExtended Object,
['nome 3'] = testExtended Object
)
[1] => 'categoria 2' = array(
['nome 2'] = test Object,
['nome 4'] = testExtended Object
)
[2] => 'categoria 3' = array(
['nome 1'] = test Object,
['nome 5'] = testExtended Object
)
[3] => 'categoria 4' = array(
['nome 6'] = testExtended Object
)
);
Quando inizializzo testContainer tramite la classe testManage $obj = new testManage();, tramite la query che ho scritto inserisco tutte le categorie nell'array; dopodicchè creo gli oggetti all'interno. Se il nome non è stato creato precedentemente vorrei creare un testExtended Object, in caso contrario un test Object.
Per farlo pensavo di mettere nella function setTest() in qualche modo una referenza del nome in modo tale da poter effettuare controlli più facilmente: un qualcosa del tipo:
Codice PHP:
<?php
testContainer = array(
[0] => 'categoria 1' = array(
['nome 1'] = testExtended Object,
['nome 2'] = testExtended Object,
['nome 3'] = testExtended Object
)
[1] => 'categoria 2' = array(
['nome 2'] = test Object,
['nome 4'] = testExtended Object
)
[2] => 'categoria 3' = array(
['nome 1'] = test Object,
['nome 5'] = testExtended Object
)
[3] => 'categoria 4' = array(
['nome 6'] = testExtended Object
)
['_inserted'] = array(
'nome 1',
'nome 2',
'nome 3',
'nome 4',
'nome 5',
'nome 6'
)
);
?>
Come scrivereste quella funzione? Oppure devo proprio gestire il tutto diversamente?