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

PHP实体层基础类

WBOY
Libérer: 2016-06-13 10:17:30
original
1034 Les gens l'ont consulté

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>
Copier après la connexion

实体层:

<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>
Copier après la connexion

使用:

<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));
Copier après la connexion

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal