For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > May 2004 > Use of variables in classes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Use of variables in classes
Michael

2004-05-18, 10:33 am

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;
}
}


Tony Marston

2004-05-18, 1:30 pm

Take a look at http://www.tonymarston.co.uk/php-my...aseobjects.html
for some tips on this subject.

--
Tony Marston

http://www.tonymarston.net


"Michael" <letters@britishlibrary.net> wrote in message
news:c8d3lk$eu7$1@hercules.btinternet.com...
> 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;
> }
> }
>
>



Chung Leong

2004-05-18, 7:30 pm

"Michael" <letters@britishlibrary.net> wrote in message
news:c8d3lk$eu7$1@hercules.btinternet.com...
> 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
>


Why would you want to do that though? The whole point of declarative
languages like SQL is to avoid the kind of complex code that you're writing.


Michael

2004-05-20, 12:30 am

I have a form which allows people to search a database of tutorials by any
combination of location, tutor, subject or date.
Using this class I can set a number of columns, tables and 'WHERE' clauses
that re-occur in every possible search when I initiate the class, then
simply add to the query as necesary for each possible search criteria
combination. Although in terms of code the class is more complex than a
series of conditional statements, by abstracting it, it makes it the code on
the page much easier to understand and therefore easier and quicker to
update.


"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:HpednQBY75qiFDfdRVn-gQ@comcast.com...
> "Michael" <letters@britishlibrary.net> wrote in message
> news:c8d3lk$eu7$1@hercules.btinternet.com...
>
use[color=darkred]
>
> Why would you want to do that though? The whole point of declarative
> languages like SQL is to avoid the kind of complex code that you're

writing.
>
>



Chung Leong

2004-05-20, 12:30 am

"Michael" <letters@britishlibrary.net> wrote in message
news:c8h7ar$hh3$1@sparta.btinternet.com...
> I have a form which allows people to search a database of tutorials by any
> combination of location, tutor, subject or date.
> Using this class I can set a number of columns, tables and 'WHERE' clauses
> that re-occur in every possible search when I initiate the class, then
> simply add to the query as necesary for each possible search criteria
> combination. Although in terms of code the class is more complex than a
> series of conditional statements, by abstracting it, it makes it the code

on
> the page much easier to understand and therefore easier and quicker to
> update.


Abstraction would mean that the calling code isn't aware of the underlying
storage mechanism. This is clearly not the case here. All you have is a
class that limits what kind of query you can build.


Michael

2004-05-20, 10:32 am

Good point. Clearly I did not explain myself very well.
I don't see what you are getting at though - Are you suggesting that there
is a way to generate multiple related query statements just using SQL?
Michael

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:TJednU4Ir7vDujHdRVn-gQ@comcast.com...
> "Michael" <letters@britishlibrary.net> wrote in message
> news:c8h7ar$hh3$1@sparta.btinternet.com...
any[color=darkred]
clauses[color=darkred]
code[color=darkred]
> on
>
> Abstraction would mean that the calling code isn't aware of the underlying
> storage mechanism. This is clearly not the case here. All you have is a
> class that limits what kind of query you can build.
>
>



Michael

2004-05-20, 1:31 pm

Dear Tony,
Thanks - Useful advice.
Michael

"Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
news:c8dd62$s7k$1$8300dec7@news.demon.co.uk...
> Take a look at http://www.tonymarston.co.uk/php-my...aseobjects.html
> for some tips on this subject.
>
> --
> Tony Marston
>
> http://www.tonymarston.net
>
>
> "Michael" <letters@britishlibrary.net> wrote in message
> news:c8d3lk$eu7$1@hercules.btinternet.com...
>
use[color=darkred]
>
>



Chung Leong

2004-05-20, 7:30 pm


"Michael" <letters@britishlibrary.net> wrote in message
news:c8ic8m$2sq$1@sparta.btinternet.com...
> Good point. Clearly I did not explain myself very well.
> I don't see what you are getting at though - Are you suggesting that there
> is a way to generate multiple related query statements just using SQL?
> Michael
>


All I'm saying you don't need a class to build a SQL statement.


Michael

2004-05-20, 9:30 pm

You may be right - it works for me though - I wouldn't use it if I was just
building one query statement, but for a series of related statements, it
looks a lot better than, for example, the code below which was what
persuaded me to write a class when faced with a similar problem to solve for
another client - in my opinion interspersing lots of conditional statements
is a messy solution compared to the relative 'cleanness' of code that you
can get by using a class. I appreciate that using a class will actually mean
that the code will take longer to run than the example below, but I
personally I'm happy to accept a slight loss of speed if it means that the
code is easy to read and update.


$query = "SELECT ev.prd_id, UNIX_TIMESTAMP(date_start) AS date_start,
UNIX_TIMESTAMP(date_end) AS date_end, evd.eve_name, evd.eve_name2,
evd.eve_des_x, t.tut_name, t.tut_url, v.ven_name, ev.ctc_id, ctc.ctc_name,
v.ven_id, v.ven_town, v.ven_url, co.country_name, prc.price_unitcost,
prd.prd_forsale, dt.date_more_x, tl.tut_priority, tl.tut_more_x";

if ($pi) {$query .= ', dsc.eve_description';}

$query .= " FROM n_event ev, n_eventdetails evd, n_date dt, n_tutor t,
n_tutorlist tl, n_venue v, n_country co, n_price prc, n_product prd,
n_contacts ctc";

if ($sbj) {$query .= ', n_event_subject esbj';}

if ($pi) {$query .= ', n_eventdescription dsc';}

$query .= " WHERE prd.cat_id=1 AND prd.prd_id = ev.prd_id AND evd.eve_id =
ev.eve_id AND dt.prd_id = ev.prd_id AND tl.prd_id = ev.prd_id AND t.tut_id =
tl.tut_id AND v.ven_id = ev.ven_id AND co.country_id = v.country_id AND
prc.price_id = prd.price_id AND ev.ctc_id = ctc.ctc_id AND dt.date_priority
= 1 ";

if ((!$isildur) && (!$tpi)) { $query.=' AND UNIX_TIMESTAMP(date_end) >
(UNIX_TIMESTAMP(now())-86400) '; }

if ($tpi) { $query .= ' AND prd.prd_id = '.$tpi.' '; }
else {$query .= ' AND prd.prd_forsale = 1 ';}

if ($t) $query .= ' AND tl.tut_id ='.$t.' ';
else $query .= ' AND tl.tut_priority = 1';

if ($pi) $query .= ' AND dsc.eve_id = ev.eve_id';


Chung Leong

2004-05-21, 10:30 pm

And exactly how you wouldn't need if statement if you use the class? All it
does is implode the conditions together, which is perfectly doable without
the use of a class.

$conds[] = ($tpi) ? "prd.prd_id = $tpi" : "prd.prd_forsale = 1";
$conds[] = ($t) ? "tl.tut_id = $t" : "tl.tut_priority = 1";
$conds[] = ($pi) ? "dsc.eve_id = ev.eve_id" : null;
....
implode(" AND ", array_filter($conds));

The point I'm trying to make is that you're encapsulating at the wrong
level. Imploding a bunch of strings and attaching "AND" and "WHERE" is easy
stuff. The complexity is in the logic of the query not its syntax.


"Michael" <letters@britishlibrary.net> wrote in message
news:c8jhg2$epb$1@hercules.btinternet.com...
> You may be right - it works for me though - I wouldn't use it if I was

just
> building one query statement, but for a series of related statements, it
> looks a lot better than, for example, the code below which was what
> persuaded me to write a class when faced with a similar problem to solve

for
> another client - in my opinion interspersing lots of conditional

statements
> is a messy solution compared to the relative 'cleanness' of code that you
> can get by using a class. I appreciate that using a class will actually

mean
> that the code will take longer to run than the example below, but I
> personally I'm happy to accept a slight loss of speed if it means that the
> code is easy to read and update.
>
>
> $query = "SELECT ev.prd_id, UNIX_TIMESTAMP(date_start) AS date_start,
> UNIX_TIMESTAMP(date_end) AS date_end, evd.eve_name, evd.eve_name2,
> evd.eve_des_x, t.tut_name, t.tut_url, v.ven_name, ev.ctc_id, ctc.ctc_name,
> v.ven_id, v.ven_town, v.ven_url, co.country_name, prc.price_unitcost,
> prd.prd_forsale, dt.date_more_x, tl.tut_priority, tl.tut_more_x";
>
> if ($pi) {$query .= ', dsc.eve_description';}
>
> $query .= " FROM n_event ev, n_eventdetails evd, n_date dt, n_tutor t,
> n_tutorlist tl, n_venue v, n_country co, n_price prc, n_product prd,
> n_contacts ctc";
>
> if ($sbj) {$query .= ', n_event_subject esbj';}
>
> if ($pi) {$query .= ', n_eventdescription dsc';}
>
> $query .= " WHERE prd.cat_id=1 AND prd.prd_id = ev.prd_id AND evd.eve_id =
> ev.eve_id AND dt.prd_id = ev.prd_id AND tl.prd_id = ev.prd_id AND t.tut_id

=
> tl.tut_id AND v.ven_id = ev.ven_id AND co.country_id = v.country_id AND
> prc.price_id = prd.price_id AND ev.ctc_id = ctc.ctc_id AND

dt.date_priority
> = 1 ";
>
> if ((!$isildur) && (!$tpi)) { $query.=' AND UNIX_TIMESTAMP(date_end) >
> (UNIX_TIMESTAMP(now())-86400) '; }
>
> if ($tpi) { $query .= ' AND prd.prd_id = '.$tpi.' '; }
> else {$query .= ' AND prd.prd_forsale = 1 ';}
>
> if ($t) $query .= ' AND tl.tut_id ='.$t.' ';
> else $query .= ' AND tl.tut_priority = 1';
>
> if ($pi) $query .= ' AND dsc.eve_id = ev.eve_id';
>
>



Michael

2004-05-22, 11:30 pm

Well, to be honest I did not mean to suggest that I would not need if
statements, only that I would not need to have them interspersed in quite
such a messy way. What you've written does look very clean, so I have to
concede that using a class for this is overkill!
Sadly, I'm still not much wiser about the use of variables in classes. You
wouldn't happen to know anything about them would you?
Michael

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:UK-dnW3mWsyOMzPd4p2dnA@comcast.com...
> And exactly how you wouldn't need if statement if you use the class? All

it
> does is implode the conditions together, which is perfectly doable without
> the use of a class.
>
> $conds[] = ($tpi) ? "prd.prd_id = $tpi" : "prd.prd_forsale = 1";
> $conds[] = ($t) ? "tl.tut_id = $t" : "tl.tut_priority = 1";
> $conds[] = ($pi) ? "dsc.eve_id = ev.eve_id" : null;
> ...
> implode(" AND ", array_filter($conds));
>
> The point I'm trying to make is that you're encapsulating at the wrong
> level. Imploding a bunch of strings and attaching "AND" and "WHERE" is

easy
> stuff. The complexity is in the logic of the query not its syntax.
>
>
> "Michael" <letters@britishlibrary.net> wrote in message
> news:c8jhg2$epb$1@hercules.btinternet.com...
> just
> for
> statements
you[color=darkred]
> mean
the[color=darkred]
ctc.ctc_name,[color=darkred]
=[color=darkred]
t.tut_id[color=darkred]
> =
AND[color=darkred]
> dt.date_priority
>
>



Chung Leong

2004-05-23, 6:30 pm

The goal is to write code that is clear, not just looks clean.

Doing with variables in classes is fairly simple: you have to use $this->
all the time.

"Michael" <letters@britishlibrary.net> wrote in message
news:c8p2f4$shp$1@hercules.btinternet.com...
> Well, to be honest I did not mean to suggest that I would not need if
> statements, only that I would not need to have them interspersed in quite
> such a messy way. What you've written does look very clean, so I have to
> concede that using a class for this is overkill!
> Sadly, I'm still not much wiser about the use of variables in classes. You
> wouldn't happen to know anything about them would you?
> Michael
>
> "Chung Leong" <chernyshevsky@hotmail.com> wrote in message
> news:UK-dnW3mWsyOMzPd4p2dnA@comcast.com...
> it
without[color=darkred]
> easy
it[color=darkred]
solve[color=darkred]
> you
actually[color=darkred]
> the
> ctc.ctc_name,
evd.eve_id[color=darkred]
> =
> t.tut_id
> AND
>
>
>



Michael

2004-05-23, 8:30 pm

> Doing with variables in classes is fairly simple: you have to use $this->
> all the time.


That's one of the parts I'm about - clearly there are a lot of
instances
where variables are refered to just using $varname rather than $this ->
varname
within classes.
Michael


Tony Marston

2004-05-24, 7:32 am

If you refer to $varname within a class method then that variable is local
only to that method.

If you refer to $this->varname then that variable is global to any method
within the class.

--
Tony Marston
http://www.tonymarston.net


"Michael" <letters@britishlibrary.net> wrote in message
news:c8ra93$kbp$1@sparta.btinternet.com...
$this->[color=darkred]
>
> That's one of the parts I'm about - clearly there are a lot of
> instances
> where variables are refered to just using $varname rather than $this ->
> varname
> within classes.
> Michael
>
>



Michael

2004-05-24, 11:32 am

Thanks Tony. I'm sure I'm being a bit thick here. I've only just started
attempting to write classes and I'm not sure why I'm finding them so
difficult.
Do you only declare with 'var = $varname' if it's a variable that you need
to have as a global within the class?
Mike

"Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
news:c8shkg$k6l$1$8300dec7@news.demon.co.uk...
> If you refer to $varname within a class method then that variable is local
> only to that method.
>
> If you refer to $this->varname then that variable is global to any method
> within the class.
>
> --
> Tony Marston
> http://www.tonymarston.net
>
>
> "Michael" <letters@britishlibrary.net> wrote in message
> news:c8ra93$kbp$1@sparta.btinternet.com...
> $this->
>
>



Tony Marston

2004-05-24, 7:30 pm

No. Just var $varname;
Take a look at http://www.tonymarston.co.uk/php-my...aseobjects.html
for a brief desciption on how to use classes in PHP.

--
Tony Marston

http://www.tonymarston.net


"Michael" <letters@britishlibrary.net> wrote in message
news:c8svd7$gri$1@titan.btinternet.com...
> Thanks Tony. I'm sure I'm being a bit thick here. I've only just started
> attempting to write classes and I'm not sure why I'm finding them so
> difficult.
> Do you only declare with 'var = $varname' if it's a variable that you need
> to have as a global within the class?
> Mike
>
> "Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
> news:c8shkg$k6l$1$8300dec7@news.demon.co.uk...
local[color=darkred]
method[color=darkred]
of[color=darkred]
$this ->[color=darkred]
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com