Home > php教程 > php手册 > PHP实体层基础类

PHP实体层基础类

WBOY
Release: 2016-06-13 10:17:30
Original
1054 people have browsed it

PHP实体层基础类

<span class</span><span  BaseModel
    {
        </span><span private</span> <span $tableName</span><span ;
        </span><span private</span> <span $fields</span>=<span array</span><span ();
        </span><span function</span><span  __construct()
        {
            </span><span $this</span>->tableName=<span rtrim</span>(<span get_class</span>(<span $this</span>),'M'<span );
        }
        </span><span function</span> __get(<span $n</span><span )
        {
            </span><span return</span>(<span $this</span>-><span $n</span><span );
        }
        </span><span function</span> __set(<span $n</span>, <span $value</span><span )
        {
            </span><span $this</span>-><span $n</span> = <span $value</span><span ;
            </span><span $this</span>->fields[<span $n</span>]=<span $value</span><span ;
        }
</span>
        <span function</span><span  Add()
        {
            </span><span $f</span>=''<span ;
            </span><span $f1</span>=''<span ;
            </span><span $arrKeys</span>=<span array_keys</span>(<span $this</span>-><span fields);
            </span><span foreach</span>(<span $arrKeys</span> <span as</span> <span $row</span><span )
            {
                </span><span $f</span>.=<span $row</span>.','<span ;
                </span><span $f1</span>.=':'.<span $row</span>.','<span ;
            }
            </span><span $f</span>=<span rtrim</span>(<span $f</span>,','<span );
            </span><span $f1</span>=<span rtrim</span>(<span $f1</span>,','<span );
            </span><span $sql</span>='insert into '.<span $this</span>->tableName.'('.<span $f</span>.') values('.<span $f1</span>.')'<span ;
            </span><span //</span><span echo $sql;</span>
            <span global</span> <span $dbh</span><span ;
            </span><span return</span> <span $dbh</span>->edit(<span $sql</span>,<span $this</span>-><span fields);
        }
</span>
        <span function</span> Update(<span $sql_where</span>,<span $params</span>=<span array</span><span ())
        {
            </span><span $f</span>=''<span ;
            </span><span $f1</span>=''<span ;
            </span><span $arrKeys</span>=<span array_keys</span>(<span $this</span>-><span fields);        
            </span><span foreach</span>(<span $arrKeys</span> <span as</span> <span $row</span><span )
            {
                </span><span $f</span>.=<span $row</span>.'=:'.<span $row</span>.','<span ;
            }
            </span><span $f</span>=<span rtrim</span>(<span $f</span>,','<span );
            </span><span $sql</span>='update '.<span $this</span>->tableName.' set '.<span $f</span>.' where '.<span $sql_where</span><span ;
            
            </span><span $arrKeys1</span>=<span array_keys</span>(<span $params</span><span );
            </span><span foreach</span>(<span $arrKeys1</span> <span as</span> <span $row</span><span )
            {
                </span><span $this</span>->fields[<span $row</span>]=<span $params</span>[<span $row</span><span ];
            }
            
            </span><span global</span> <span $dbh</span><span ;
            </span><span return</span> <span $dbh</span>->Edit(<span $sql</span>,<span $this</span>-><span fields);
        }
        
        </span><span function</span> Delete(<span $sql_where</span>,<span $params</span>=<span array</span><span ())
        {
            </span><span $sql</span>='delete from '.<span $this</span>->tableName.' where '.<span $sql_where</span><span ;
            </span><span //</span><span echo $sql;</span>
            
            <span global</span> <span $dbh</span><span ;
            </span><span return</span> <span $dbh</span>->Edit(<span $sql</span>,<span $params</span><span );
        }

    }</span>
Copy after login

实体层:

<span class</span> proM <span extends</span><span  BaseModel
{</span>
    <span private</span> <span $proName</span>;
    <span private</span> <span $proDetail</span>;
    <span private</span> <span $proPic</span>;
    <span private</span> <span $proPic1</span>;
    <span private</span> <span $proAttr</span>;
    <span private</span> <span $proTaxis</span>;
    <span private</span> <span $proIsHome</span>;
    <span private</span> <span $proIsTJ</span>;
    <span private</span> <span $proIsCTJ</span>;
    <span private</span> <span $proIsDel</span>;
    <span private</span> <span $proAddTime</span>;
    <span private</span> <span $proisHot</span>;
    <span private</span> <span $proisPorClass</span>;
    <span private</span> <span $proisYN</span>;
    <span private</span> <span $proGL</span>;
    <span private</span> <span $proSeoT</span>;
    <span private</span> <span $proSeoK</span>;
    <span private</span> <span $proSeoD</span>;
    <span private</span> <span $proBH</span><span ;
    </span><span private</span> <span $FK_proClass</span><span ;
    </span><span private</span> <span $proModel</span><span ;
    </span><span private</span> <span $proPrice</span><span ;
    </span><span private</span> <span $proDiscount</span><span ;
    </span><span private</span> <span $proHit</span><span ;
    </span><span private</span> <span $proURL</span><span ;
    </span><span private</span> <span $proisKdWords</span><span ;
    </span><span private</span> <span $proisKdDone</span><span ;
    </span><span private</span> <span $proFL</span><span ;
}</span>
Copy after login

使用:

<span $m</span>=<span new</span><span  proM();
</span><span $m</span>->proName='代码'<span ;
</span><span $m</span>->proDetail='php开发'<span ;
</span><span $m</span>->Update('proID=:proID',<span array</span>(proID=>1));
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template