> 백엔드 개발 > PHP 튜토리얼 > PHP는 테이블 구조를 읽고 자동으로 PHP 클래스를 생성합니다.

PHP는 테이블 구조를 읽고 자동으로 PHP 클래스를 생성합니다.

WBOY
풀어 주다: 2016-07-28 08:28:56
원래의
1172명이 탐색했습니다.

평소 프로젝트를 작성하다 보면 데이터베이스 운영에 대해 자주 접하게 되는데, 데이터베이스를 운영할 때마다 가장 귀찮은 것은 규칙에 따라 자동 생성되는 작은 데모를 작성하는 것입니다.

<code><span><?php</span><span>// 数据库配置参数</span><span>$db_config</span> = <span>array</span>(
    <span>'host'</span> => <span>'127.0.0.1'</span>,
    <span>'port'</span> => <span>3306</span>,
    <span>'username'</span> => <span>'root'</span>,
    <span>'password'</span> => <span>''</span>,
    <span>'dbname'</span> => <span>'joe_db'</span>,
    <span>'charset'</span> => <span>'utf8'</span>
);

<span>$tablePre</span> = <span>'joe_'</span>; <span>// 表前缀</span><span>$tableName</span> = <span>'users'</span>; <span>// 表名</span><span>$res</span> = linkdb(<span>$db_config</span>, <span>$tablePre</span> . <span>$tableName</span>);

<span>$className</span> = <span>$tableName</span> . <span>'Dmodel'</span>;
<span>$fileName</span> = <span>$tableName</span> . <span>'.dm.class.php'</span>;
<span>$annStr</span> = addAnnotation(<span>$fileName</span>, <span>$tableName</span>, <span>'用户session表'</span>);
<span>$classStr</span> = readTableStr(<span>$res</span>, <span>$className</span>,<span>$annStr</span>);

<span>// 写入文件</span><span>$file</span> = fopen(<span>$fileName</span>, <span>"w+"</span>);
fwrite(<span>$file</span>, <span>$classStr</span>);
fclose(<span>$file</span>);

<span>/**
 * 根据表结构初始化表对象
 *
 *<span> @param</span> unknown $res            
 *<span> @param</span> unknown $className            
 *<span> @return</span> string
 */</span><span><span>function</span><span>readTableStr</span><span>(<span>$res</span>, <span>$className</span>, <span>$annStr</span>=<span>''</span>)</span>
{</span><span>$result</span> = <span>'<?php '</span> . <span>"\n"</span>;
    <span>$result</span> .= <span>$annStr</span>;
    <span>$result</span> .= <span>"class $className \n{"</span>;

    <span>// 变量</span><span>foreach</span> (<span>$res</span><span>as</span><span>$v</span>) {
        <span>$result</span> .= <span>"\n"</span> . <span>'private $_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>";//"</span> . <span>$v</span>[<span>'Comment'</span>] . <span>$v</span>[<span>'Type'</span>];
    }

    <span>$result</span> .= <span>"\n"</span>;

    <span>// get set</span><span>foreach</span> (<span>$res</span><span>as</span><span>$v</span>) {
        <span>$result</span> .= <span>"\n public function set"</span> . <span>$v</span>[<span>'Field'</span>] . <span>'($_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>"){"</span>;
        <span>$result</span> .= <span>"\n"</span> . <span>' $this->_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>'=$_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>";"</span>;
        <span>$result</span> .= <span>"\n }"</span>;

        <span>$result</span> .= <span>"\n public function get"</span> . <span>$v</span>[<span>'Field'</span>] . <span>'(){'</span>;
        <span>$result</span> .= <span>"\n"</span> . <span>' return $this->_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>";"</span>;
        <span>$result</span> .= <span>"\n }"</span>;

        <span>$result</span> .= <span>"\n"</span>;
    }

    <span>$result</span> .= <span>" \n }"</span>;
    <span>$result</span> .= <span>" \n ?>"</span>;

    <span>return</span><span>$result</span>;
}

<span>/**
 * 添加类注释
 * 
 *<span> @param</span> unknown $fileName            
 *<span> @param</span> unknown $fun            
 *<span> @param</span> unknown $des            
 *<span> @param</span> unknown $date            
 *<span> @param</span> unknown $author            
 */</span><span><span>function</span><span>addAnnotation</span><span>(<span>$fileName</span>, <span>$fun</span>, <span>$des</span>)</span>
{</span><span>$annStr</span> =  <span>"\n/**"</span>;
    <span>$annStr</span> .= <span>"\n* 文件名:"</span> . <span>$fileName</span>;
    <span>$annStr</span> .= <span>"\n* 功能:    模型层-表-"</span> . <span>$fun</span>;
    <span>$annStr</span> .= <span>"\n* 描述:    "</span> . <span>$des</span>;
    <span>$annStr</span> .= <span>"\n* 日期:    "</span> . date(<span>'y-m-d'</span>, time());
    <span>$annStr</span> .= <span>"\n* 版权:    Copyright ? 2016 github.com/JoeXiong All Rights Reserved"</span>;
    <span>$annStr</span> .= <span>"\n* @author JoeXiong"</span>;
    <span>$annStr</span> .= <span>"\n*/"</span>;
    <span>return</span><span>$annStr</span>;
}

<span>/**
 * * 连接数据库,查询表结构
 *
 *<span> @param</span> unknown $array
 *            数据库连接参数
 *<span> @param</span> unknown $tableName
 *            表名
 */</span><span><span>function</span><span>linkdb</span><span>(<span>$array</span>, <span>$tableName</span>)</span>
{</span><span>$mysql_server_name</span> = <span>$array</span>[<span>'host'</span>]; <span>// 改成自己的mysql数据库服务器</span><span>$mysql_username</span> = <span>$array</span>[<span>'username'</span>]; <span>// 改成自己的mysql数据库用户名</span><span>$mysql_password</span> = <span>$array</span>[<span>'password'</span>]; <span>// 改成自己的mysql数据库密码</span><span>$mysql_database</span> = <span>$array</span>[<span>'dbname'</span>]; <span>// 改成自己的mysql数据库名</span><span>$conn</span> = mysql_connect(<span>$mysql_server_name</span>, <span>$mysql_username</span>, <span>$mysql_password</span>) <span>or</span><span>die</span>(<span>"error connecting"</span>); <span>// 连接数据库</span>
    mysql_query(<span>"set names "</span> . <span>$array</span>[<span>'charset'</span>]); <span>// 数据库输出编码</span>
    mysql_select_db(<span>$mysql_database</span>); <span>// 打开数据库</span><span>$sql</span> = <span>"SHOW FULL COLUMNS FROM $tableName"</span>;
    <span>$res</span> = mysql_query(<span>$sql</span>);
    <span>$res</span> = dataTable(<span>$res</span>);

    <span>return</span><span>$res</span>;
}

<span>/**
 * 返回到表集合
 *
 *<span> @param</span> query $query            
 *<span> @return</span> array
 */</span><span><span>function</span><span>dataTable</span><span>(<span>$query</span>)</span>
{</span><span>if</span> (<span>$query</span>) {
        <span>$ListTable</span> = <span>array</span>();
        <span>while</span> (<span>$rows</span> = mysql_fetch_array(<span>$query</span>, MYSQL_ASSOC)) {
            array_push(<span>$ListTable</span>, <span>$rows</span>);
        }
        <span>return</span><span>$ListTable</span>;
    } <span>else</span> {
        <span>return</span><span>0</span>;
    }
}

<span>/**
 * 调试函数
 *
 *<span> @param</span> unknown $msg            
 */</span><span><span>function</span><span>show_bug</span><span>(<span>$msg</span>)</span>
{</span><span>echo</span><span>'<pre class="brush:php;toolbar:false">'</span>;
    var_dump(<span>$msg</span>);
    <span>echo</span><span>'
'; }
로그인 후 복사

최종 효과:

<code><span><?php</span><span>/**
* 文件名:users.dm.class.php
* 功能:    模型层-表-users
* 描述:    用户session表
* 日期:    16-06-27
* 版权:    Copyright ? 2016 github.com/JoeXiong All Rights Reserved
*<span> @author</span> JoeXiong
*/</span><span><span>class</span><span>usersDmodel</span>
{</span><span>private</span><span>$_userid</span>; <span>// int(11) unsigned</span><span>private</span><span>$_username</span>; <span>// 用户名varchar(50)</span><span>private</span><span>$_password</span>; <span>// 密码varchar(32)</span><span>private</span><span>$_email</span>; <span>// 邮箱varchar(80)</span><span>private</span><span>$_mobile</span>; <span>// 手机号码varchar(20)</span><span>private</span><span>$_ip</span>; <span>// 用户注册IDvarchar(30)</span><span>private</span><span>$_status</span>; <span>// 帐号状态tinyint(1) unsigned</span><span>private</span><span>$_areaid</span>; <span>// 区域Id,10001为本区;10002为qq用户;10003为新浪微博用户smallint(5) unsigned</span><span>private</span><span>$_addtime</span>; <span>// 创建时间int(10) unsigned</span><span>public</span><span><span>function</span><span>setUserId</span><span>(<span>$_userid</span>)</span>
    {</span><span>$this</span>->_userid = <span>$_userid</span>;
    }

    <span>public</span><span><span>function</span><span>getUserId</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_userid;
    }

    <span>public</span><span><span>function</span><span>setUserName</span><span>(<span>$_username</span>)</span>
    {</span><span>$this</span>->_username = <span>$_username</span>;
    }

    <span>public</span><span><span>function</span><span>getUserName</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_username;
    }

    <span>public</span><span><span>function</span><span>setPassWord</span><span>(<span>$_password</span>)</span>
    {</span><span>$this</span>->_password = <span>$_password</span>;
    }

    <span>public</span><span><span>function</span><span>getPassWord</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_password;
    }

    <span>public</span><span><span>function</span><span>setEmail</span><span>(<span>$_email</span>)</span>
    {</span><span>$this</span>->_email = <span>$_email</span>;
    }

    <span>public</span><span><span>function</span><span>getEmail</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_email;
    }

    <span>public</span><span><span>function</span><span>setMobile</span><span>(<span>$_mobile</span>)</span>
    {</span><span>$this</span>->_mobile = <span>$_mobile</span>;
    }

    <span>public</span><span><span>function</span><span>getMobile</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_mobile;
    }

    <span>public</span><span><span>function</span><span>setIP</span><span>(<span>$_ip</span>)</span>
    {</span><span>$this</span>->_ip = <span>$_ip</span>;
    }

    <span>public</span><span><span>function</span><span>getIP</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_ip;
    }

    <span>public</span><span><span>function</span><span>setStatus</span><span>(<span>$_status</span>)</span>
    {</span><span>$this</span>->_status = <span>$_status</span>;
    }

    <span>public</span><span><span>function</span><span>getStatus</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_status;
    }

    <span>public</span><span><span>function</span><span>setAreaId</span><span>(<span>$_areaid</span>)</span>
    {</span><span>$this</span>->_areaid = <span>$_areaid</span>;
    }

    <span>public</span><span><span>function</span><span>getAreaId</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_areaid;
    }

    <span>public</span><span><span>function</span><span>setAddTime</span><span>(<span>$_addtime</span>)</span>
    {</span><span>$this</span>->_addtime = <span>$_addtime</span>;
    }

    <span>public</span><span><span>function</span><span>getAddTime</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_addtime;
    }
}
<span>?></span></span></code>
로그인 후 복사
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

위 내용은 PHP가 테이블 구조를 읽고 관련 내용을 포함하여 자동으로 PHP 클래스를 생성하는 방법을 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿