这是我对OO PHP的第一种方法,我创建了一个Person类,我需要通过查询数据库或POST值来创建Person对象。然后我需要将数据保存到DB。
这是我的代码,我不知道这是不是正确的方法。我需要一些建议。
class Persona {
protected $id=NULL;
protected $nome;
protected $cognome;
protected $cf=NULL;
protected $indirizzo=NULL;
protected $civico=NULL;
protected $citta=NULL;
protected $cap=NULL;
protected $provincia=NULL;
protected $nazione=NULL;
protected $telefono=NULL;
protected $fax=NULL;
protected $cellulare=NULL;
protected $email;
protected $data_registrazione;
protected $tipo_registrazione;
public function createPersona($postData=NULL,$id=NULL,$email=NULL)
{
global $_CONFIG;
if(is_array($postData) && isset($postData['nome']) && isset($postData['cognome']) && isset($postData['email']) && isset($postData['tipo_registrazione']))
{
$record=$postData;
}elseif(isset($id)){
$result=mysql_query("SELECT * FROM ".$_CONFIG['tbl_persone']." WHERE id='".escape_string($id)."'");
if(mysql_num_rows($result)!=1) return false;
$record = mysql_fetch_assoc($result);
}elseif(isset($email)){
$result=mysql_query("SELECT * FROM ".$_CONFIG['tbl_persone']." WHERE email='".strtolower(escape_string($email))."'");
if(mysql_num_rows($result)!=1) return false;
$record = mysql_fetch_assoc($result);
}else{
return false;
}
if(isset($record['cf'])) $record['cf']=strtoupper($record['cf']);
if(isset($record['cap'])) $record['cap']=strtoupper($record['cap']);
if(!isset($record['nazione']) && isset($record['prefisso'])) $record['nazione']=$record['prefisso'];
$record['email']=strtolower($record['email']);
if(!isset($record['data_registrazione'])) $record['data_registrazione']=date('Y-m-d H:i:s');
$vars=get_object_vars($this);
foreach($vars as $key=>$value)
{
if(isset($record[$key])){$this->$key=$record[$key];}
}
if(!$this->validatePersona())return false;
return true;
}
protected function validatePersona()
{
if(isset($this->id) && !validateID($this->id)) return false;
if(isset($this->cf) && !validateCF($this->cf)) return false;
if(isset($this->cap) && !validateCAP($this->cap)) return false;
if(isset($this->email) && !validateEmail($this->email)) return false;
return true;
}
public function savePersona()
{
global $_CONFIG;
$vars=get_object_vars($this);
foreach($vars as $key=>$value)
{
if($key!='id')
{
if(isset($this->$key))
{
$columns.=$key.",";
$values.="'".escape_string($this->$key)."',";
}
}
}
if(!mysql_query("INSERT INTO ".$_CONFIG['tbl_persone']." (".substr($columns,0,-1).") VALUES (".substr($values,0,-1).")"))
{
return false;
}else{
return true;
}
}
}
$p=new Persona();
if(!$p->createPersona($_POST)){
echo 'Si è verificato un errore.<br />Riprova più tardi. [0]';
exit;
}
if($p->createPersona(NULL,NULL,$_POST['email'])){
echo 'Indirizzo email già registrato.';
exit;
}
if(!$p->savePersona()){
echo 'Si è verificato un errore.<br />Riprova più tardi. [2]';
exit;
}
第二步是创建一个动态HTML表,其中包含来自我的数据库的人员数据,现在通过过程语言我获取数据库并创建一个数组,然后使用foreach结构我打印表,但我不知道如何用OO语言。
谢谢大家
弗朗西斯
答案 0 :(得分:1)
PDO
。 mysql
驱动程序已过时,非常不鼓励使用。此外,PDO
完全是OOP。在线阅读一些教程。protected $id=NULL;
您不必为未启动的属性显式指定null。此外,如果您知道要从外部访问属性(即通过对象),请使属性public
:public $id;
就足够了。 protected
的目的是,如果您希望某些属性可用于从主类派生的类(=子类)。global
。使用带常量的config类或从包含在单独文件中的数组中读取设置。您可以将该数组传递给类构造函数。__contruct
,因此只需按照以下方式执行:$persona = new Persona($_POST);
同时写下null
代替NULL
,它更清晰,效率更高。
function __construct(Array $postData = array(), $id = null, $email = null) {
}
答案 1 :(得分:0)
@silkfire @ DevZer0好的,所以我编辑了我以前的代码,您怎么看?
class factoryPersona {
public function createPersona($origin,$by)
{
switch($by)
{
case 'post':
// get data from $_POST to $array
break;
case 'id':
// get data from DB to $array
break;
case 'email':
// get data from DB to $array
break;
}
$persona=new Persona($array);
if(!$persona->validatePersona())
{
// validation error
exit;
}
return $persona;
}
}
class Persona {
private $id;
private $nome;
private $cognome;
private $cf;
private $indirizzo;
private $civico;
private $citta;
private $cap;
private $provincia;
private $nazione;
private $telefono;
private $fax;
private $cellulare;
private $email;
private $data_registrazione;
private $tipo_registrazione;
public function __construct($array=null)
{
// assignments
}
public function validatePersona()
{
if(isset($this->id) && !validateID($this->id)) return false;
if(isset($this->cf) && !validateCF($this->cf)) return false;
if(isset($this->cap) && !validateCAP($this->cap)) return false;
if(isset($this->email) && !validateEmail($this->email)) return false;
return true;
}
public function savePersona()
{
// save person to DB
}
}
$persona=new factoryPersona;
$persona=$persona->createPersona($_POST,'post');
$persona->savePersona();