这是我使用 OO PHP 的第一个方法,我创建了一个 Person 类,我需要通过查询数据库或通过 POST 值来创建 Person 对象。然后我需要将数据保存到数据库。
这是我的代码,我不知道这是否是正确的方法。我需要一些建议。
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 构造打印该表,但我不知道如何使用面向对象语言。
谢谢大家
弗朗西斯科
最佳答案
- 如果要将数据库查询结果映射到对象,请使用
PDO
反而。mysql
驱动程序已过时,强烈建议不要使用。另外,PDO
是完全面向对象的。在线阅读一些教程。 -
protected $id=NULL;
您不必将 null 显式分配给未启动的属性。另外,如果您知道要从外部(即通过对象)访问属性,请将属性设置为public
:public $id;
就足够了。protected
的目的如果您希望从主类(=子类)派生的类可以使用某些属性。 - 请勿使用关键字
global
。使用带有常量的配置类或从包含在单独文件中的数组读取设置。您可以将该数组传递给类构造函数。 - 如果createPersona是你的主要构建方法,你可以将其重命名为
__contruct
因此只需这样做:$persona = new Persona($_POST);
还写null
而不是NULL
,它更干净并且效率更高。
function __construct(Array $postData = array(), $id = null, $email = null) {
}
关于php - 简单的 PHP Person 类建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18588933/