在类中使用变量 [英] Use of variables in classes
问题描述
我写了一个简单的类,它将MySQL SELECT查询放在一起,
允许你扩展查询,但我不确定何时使用$ this - >
var_name,当我可以使用$ varname时,以及何时需要使用
''var $ varname'',当这可以省略时。
这是'班级 - 可以变得更简单吗?
Michael
class select_query_builder {
//放在一起并添加到选择查询的类
var $ query;
var $ select;
var $ from ;
var $ where;
var $ orderby;
var $ distinct;
var $ t_select; < br $>
var $ t_from;
var $ t_where;
var $ t_orderby;
函数select_query_builder( $ select,$ from,$ where ='''',$ orderby =''''){
//每个变量都是一个数组
$ this - > select = $ select;
$ this - > from = $ from;
$ this - > where = $ where;
$ this - > orderby = $ orderby;
$ this - > t_select = $ select;
$ this - > t_from = $ from;
$ this - > t_where = $ where;
$ this - > t_orderby = $ orderby;
}
//在查询中添加其他元素
函数extend_query($ what,$ newinfo )
{
$ this - > {'t _''。$ what} = array_merge($ this - > {''t _''。$ what},$ newinfo);
}
//返回完整的查询;
函数return_query(){
$ this - > query =''SELECT''$ this - >不同。''''.implode('','',$ this - >
t_select)。''FROM''。implode('','',$ this - > t_from);
if(!empty($ this - > t_where)){
$ this - >查询。=''在哪里'.implode(''AND'',$ this - > t_where);
}
if(!empty ($ this - > orderby)){
$ this - > query。=''ORDER BY''。implode(''ASC,'',$ this - > t_orderby)。''
ASC'';
}
返回$ this - >查询;
}
函数reset_to_before_extend(){
$ this - > t_select = $ this - >选择;
$ this - > t_from = $ this - >来自;
$ this - > t_where = $ this - > where;
$ this - > t_orderby = $ this - > orderby;
}
}
I''ve written a simple class that puts together a MySQL SELECT query and
allows you to extend the query, but I''m unsure as to when to use $this - >
var_name and when I can just use $varname, and when it''s necessary to use
''var $varname'' and when this can be left out.
Here''s the class - could it be made simpler?
Michael
class select_query_builder {
// class to put together and add to a select query
var $query;
var $select;
var $from;
var $where;
var $orderby;
var $distinct;
var $t_select;
var $t_from;
var $t_where;
var $t_orderby;
function select_query_builder($select, $from, $where='''', $orderby ='''') {
// each variable is an array
$this -> select = $select;
$this -> from = $from;
$this -> where = $where;
$this -> orderby = $orderby;
$this -> t_select = $select;
$this -> t_from = $from;
$this -> t_where = $where;
$this -> t_orderby = $orderby;
}
// add additional elements to the query
function extend_query($what, $newinfo)
{
$this -> {''t_''.$what} = array_merge($this -> {''t_''.$what}, $newinfo);
}
// return the complete query;
function return_query() {
$this -> query = ''SELECT ''$this -> distinct.'' ''.implode('', '', $this ->
t_select).'' FROM ''.implode('', '', $this -> t_from);
if (!empty($this -> t_where)) {
$this -> query .= '' WHERE ''.implode('' AND '', $this -> t_where);
}
if (!empty($this -> orderby)) {
$this -> query .= '' ORDER BY ''.implode('' ASC, '', $this -> t_orderby).''
ASC'';
}
return $this -> query;
}
function reset_to_before_extend() {
$this -> t_select = $this -> select;
$this -> t_from = $this -> from;
$this -> t_where = $this -> where;
$this -> t_orderby = $this -> orderby;
}
}
推荐答案
this - >
var_name当我可以使用
this - >
var_name and when I can just use
varname时,以及何时需要使用
''var
varname, and when it''s necessary to use
''var
varname''什么时候可以省略。
这里是班级 - 可以变得更简单吗?
Michael
class select_query_builder {
//要放在一起并添加到选择查询的类
var
varname'' and when this can be left out.
Here''s the class - could it be made simpler?
Michael
class select_query_builder {
// class to put together and add to a select query
var
这篇关于在类中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!