vabbeh su quello avevi risposto te. Scrivere i getter e i setter è la cosa piu stupida del mondo, tanto che alcuni prodotti te la danno come possibilità (mi pare PHPStorm lo faccia), è che i plugin degli IDE (non so eclipse, ma netbeans non lo fà) non ti danno la possibilità di farlo in maniera automatica e quindi uno perde un pò di tempo (quanto? 5 minuti? ), mentre le scorciatoglie con __get e __set rende impossibile sapere dall'esterno che proprietà la classe supporta e ogni volta uno dovrebbe avere la documentazione sotto mano
oh, sarò verboso io![]()
IP-PBX management: http://www.easypbx.it
Old account: 2126 messages
Oldest account: 3559 messages
Quindi ragazzi tra old-school e new-school cosa conviene fare?
Per il discorso del model-controller penso di non aver ancora capito bene, io sono messo così adesso:
Codice PHP:abstract class Content
{
private $data;
public function __set($k, $v)
{
if(array_key_exists($k, static::$types))
{
$filtered_data = Filter::filterByType($v, static::$types[$k]);
if ($filtered_data)
$this->data[$k] = $filtered_data;
}
}
public function __get($k)
{
if(array_key_exists($k, $this->data))
return $this->data[$k];
}
public function getTypes()
{
return static::$types;
}
}
class Film extends Content
{
protected static $types = array(
'id' => 'int',
'title' => 'string',
'year' => 'int',
'actor' => 'object'
);
}
class Actor extends Content
{
protected static $types = array(
'id' => 'int',
'name' => 'string',
'surname' => 'string',
'born_year' => 'int'
);
}
Ultima modifica di iJoker; 28-01-2014 a 21:53
Questa volta, più che un voto.. è favoreggiamento.
una cosa del genere può andare bene?
Codice PHP:class DB{
private static $instance;
private $dbh;
private function __construct()
{
$this->dbh = new PDO('mysql:host=localhost;dbname=ps', 'root', '123456');
}
public function __destruct()
{
$this->dbh = NULL;
}
public static function getInstance()
{
if(self::$instance == NULL)
self::$instance = new DB();
return self::$instance;
}
public function insert($obj, $table, $types)
{
foreach ($types as $k => $v)
$columns[$k] = ($obj->__get($k)) ?: NULL;
$columns_str = implode(', ', array_keys($columns));
$placeholders_str = ':'.implode(', :', array_keys($columns));
$stmt = $this->dbh->prepare("INSERT INTO $table ($columns_str) VALUES ($placeholders_str)");
$stmt->execute($columns);
$obj->__set('id', $this->dbh->lastInsertId());
}
}
Ultima modifica di iJoker; 29-01-2014 a 02:07
Questo è un mapper...
Un controller è molto piu semplice:
un ipotetico controller per i film del tuo database.. ma secondo me devi studiare un po'Codice PHP:class filmController {
protected $mapper;
protected $view;
public function __construct( PDO $pdo, view $view ) {
$this->mapper = new Mapper( $pdo );
$this->view = $view;
}
public function index() {
$films = $this->mapper->fetchAll();
$content = new view( 'index-content' );
$content->films = $films;
$this->view->title = 'Index';
$this->view->addContent( 'content', $content );
}
public function add() {
$content = new view( 'film-add-form' );
// un metodo che usa il controller da controller
$fv = new formValidator();
$fv->validateEmpty( 'name', 'You must insert a value for the field [name]' );
$fv->validateEmpty( 'description', 'You must insert a value for the field [description]' );
if ( $fv->checkErrors() ) {
$message = new view( 'message', $fv->displayErrors(), array( 'class'=>'error-message' ) );
$content->addView( 'message', $message );
} else {
$this->mapper->insert();
$message = new view( 'message', $this->mapper->getMsg(), array( 'class'=>'service-message' ) );
$content->addView( 'message', $message );
}
$this->view->title = 'Add';
$this->view->addContent( 'content', $content );
}
}
se hai problemi chiedi pure
Questa volta, più che un voto.. è favoreggiamento.