OOP数据库表< - > php界面(半长) [英] OOP database tables <-> php interface (semi LONG)

查看:47
本文介绍了OOP数据库表< - > php界面(半长)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试掌握OOP,使用类对象构建一个接口,让我们可以轻松地访问数据库表。您之前可能已经看过这个,或者

甚至可能在某个时间点自己构建它。我已经研究了一些我在互联网上找到的一些

的例子,我现在正试图从

scratch开始构建我自己的。


我已经创建了一个默认的表类(DB_Table),一个默认的验证类

(Validator),它是DB_Table中的一个对象,我的最终数据库表

每个都得到一个类:例如扩展

DB_Table类的用户,UserProfile,Country等。


它的内容(不完整的代码,仅用于演示

目的):


<?php


class DB_Table

{

公共函数__construct()

{

//初始化对象

//初始化(通用)验证器

$ this-> validator = new Validator($ this);

}


公共函数loadData()

{

//从记录中获取所有数据

}


公共函数saveData()

{

//将所有数据保存到记录中

}


公共函数__get($ field)

{

//获取对象属性

}


公共函数__set($ field,$ value)

{

//设置对象属性

}

}


class User扩展DB_Table

{

public $ db t = DB_TABLE_USER;


public $ fields = array(

''id''= NULL,

''userType' '= NULL,

''用户名''= NULL,

''密码''= NULL,

''email''= NULL,

''active''= NULL,

''timeCreated''= NULL,

''timeActivated''= NULL,

''timeUpdated''= NULL

);


public $ fieldSpecs = array(

''id''=数组(

''screenname''=''用户ID'',

''type''=''int'',

''autoinsert''= true,

''autoincrement''= true,

''static''= true
),

''userType''=数组(

''type''=''tyniint'',

''nodisplay''= true

),

''用户名''=数组(

''screenname''=''用户名'',

't ype''='''string'',

''unique''= true,

''required''= true,

''static''= true,

),

//等......

);

}


?>


它绝对需要大量的探索才能顺利工作

对于简单的数据库表。但除此之外,还有一些未来的问题

已经困扰我了:


例如,上课用户:让我们说有人注册我的网站。

此人填写详细信息。 Validator类单独检查每个字段

。没问题。新的用户记录被插入到数据库中。


但是现在,如果用户想要在用户注册后登录到系统,那么现在是什么?

我会创建一个User类的实例。将凭证值设置为

对象。让Validator类再次完成它的工作。

但是,如果用户使用不正确的凭据登录,我不想向用户报告

是否他/她填写了错误的用户名或

不正确的密码。不,我只想报告他/她已填写

错误的凭据。


但Validator类只分别检查每个字段。为了保持

尽可能通用Validator类,你会建立一个单独的Login

类吗?一个有一些自定义验证方法的类?或者

或许只是在User类中插入自定义验证方法?我自己

在这种情况下似乎无法下定决心。可能是因为在OOP编程方面,我仍然没有经验。


还会想到一些其他的小问题:

- 我的直觉告诉我在DB_Table中引用Validator

类是明智的,例如:

$ this-> validator =& ;新的Validator($ this);

....因为它只创建了一个对象的实例,对吧?


- 为什么可以''我从扩展的

类(例如User)中引用DB_Table中的私有变量?我想,这个私有变量,在父对象的
中声明,现在也是User中的私有变量,不是吗?


- 最后但是同样重要的是,有没有办法让我从Validator类中访问用户

属性,而不必将$ this作为

参数传递给:

$ this-> validator = new Validator($ this);

....我知道以下内容不正确,但我在想什么

喜欢关键字parent。


我希望这些问题和例子对你有意义,希望有人能够对这些问题有所了解

。提前感谢您的时间。


干杯

解决方案

this-> validator = new Validator (


this);

}


公共函数loadData()

{

//从记录中获取所有数据

}


公共函数saveData()

{

//将所有数据保存到记录中

}


公共函数__get(


字段)

{

//获取对象属性

}


公共函数__set(

Hi,

I''m trying to grasp OOP to build an interface using class objects that lets
me access database tables easily. You have probably seen this before, or
maybe even built it yourself at some point in time. I have studied some
examples I found on the internet, and am now trying to build my own from
scratch.

I have made a default table class (DB_Table), a default validation class
(Validator) which is an object inside DB_Table and my final database tables
each get a class: e.g. User, UserProfile, Country, etc that extend the
DB_Table class.

Its something along the lines of (incomplete code, just for demonstration
purpose):

<?php

class DB_Table
{
public function __construct()
{
// intialize object
// initialize (common) validator
$this->validator = new Validator( $this );
}

public function loadData()
{
// get all data from record
}

public function saveData()
{
// save all data to record
}

public function __get( $field )
{
// get object property
}

public function __set( $field, $value )
{
// set object property
}
}

class User extends DB_Table
{
public $dbt = DB_TABLE_USER;

public $fields = array(
''id'' =NULL,
''userType'' =NULL,
''username'' =NULL,
''password'' =NULL,
''email'' =NULL,
''active'' =NULL,
''timeCreated'' =NULL,
''timeActivated'' =NULL,
''timeUpdated'' =NULL
);

public $fieldSpecs = array(
''id'' =array(
''screenname'' =''user id'',
''type'' =''int'',
''autoinsert'' =true,
''autoincrement'' =true,
''static'' =true
),
''userType'' =array(
''type'' =''tyniint'',
''nodisplay'' =true
),
''username'' =array(
''screenname'' =''username'',
''type'' =''string'',
''unique'' =true,
''required'' =true,
''static'' =true,
),
// etc...
);
}

?>

It definately still needs a lot of tayloring for it to even work smoothly
for straightforward DB tables. But besides that, a few future issues are
bothering me already:

For instance, take the class User: let''s say somebody registers at my site.
This person fills in the details. The Validator class checks each field
seperately. No problem. A new User record is inserted in the database.

But what now, if a user wants to login to the system after the user is
registrated?
I would create an instance of class User. Asign the credential values to the
object. And let the Validator class do its work again.
But, if a user logs in with incorrect credentials, I don''t want to report
back to the user whether he/she filled in an incorrect username or an
incorrect password. No, I only want to report back that he/she has filled in
wrong credentials.

But the Validator class only checks each field seperately. In order to keep
the Validator class as common as possible would you build a seperate Login
class for instance? A class that has some custom validation methods? Or
perhaps just insert custom validation methods in the User class? I myself
just cannot seem to make up my mind in this case. Probably cause I''m still
too inexperienced when it comes to OOP programming.

A few other minor questions come to mind also:
- My gut feeling tells me it is wise to make a reference to the Validator
class inside DB_Table, e.g:
$this->validator =& new Validator( $this );
.... because it only creates a single instance of the object, right?

- And why can''t I refer to a private variable in DB_Table from an extended
class (such as User)? I would asume, that this private variable, declared in
the parent object, is now a private variable in User also, no?

- And last but not least, is there a way for me to access let''s say User
properties from the Validator class without having to pass $this as an
argument to:
$this->validator = new Validator( $this );
.... I know the following to be incorrect, but I was thinking of something
like the keyword parent.

I hope these questions and examples make sense to you and hopefully someone
can shed a light on these issues. Thank you in advance for your time.

Cheers

解决方案

this->validator = new Validator(


this );
}

public function loadData()
{
// get all data from record
}

public function saveData()
{
// save all data to record
}

public function __get(


field )
{
// get object property
}

public function __set(


这篇关于OOP数据库表&lt; - &gt; php界面(半长)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆