php - 简单的 PHP Person 类建模

标签 php mysql database oop modeling

这是我使用 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 构造打印该表,但我不知道如何使用面向对象语言。

谢谢大家

弗朗西斯科

最佳答案

  1. 如果要将数据库查询结果映射到对象,请使用 PDO反而。 mysql驱动程序已过时,强烈建议不要使用。另外,PDO是完全面向对象的。在线阅读一些教程。
  2. protected $id=NULL;您不必将 null 显式分配给未启动的属性。另外,如果您知道要从外部(即通过对象)访问属性,请将属性设置为 public :public $id;就足够了。 protected的目的如果您希望从主类(=子类)派生的类可以使用某些属性。
  3. 请勿使用关键字 global 。使用带有常量的配置类或从包含在单独文件中的数组读取设置。您可以将该数组传递给类构造函数。
  4. 如果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/

相关文章:

PHP 多个三元运算符未按预期工作

php - 获取 Oracle 中存在但 MySQL 中缺失的记录的挑战

MySQL 静态哈希索引

mysql - 如何在 SQL 中使用 NULL 存储缺失的 FLOAT 值

ios - iOS 应用程序中的 SQlite 数据库在下载后被加密

MySQL 错误代码 : 1030Got error -1 from storage engine; I've tried to delete data from my database

php artisan migrate - SQLSTATE [HY000] [1045] 拒绝用户访问 'laravel' @'localhost'

php - Laravel 中更改密码重置邮件消息

php - 强制下载YouTube视频

database - 数据历史跟踪最佳实践