Use of variables in classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    Use of variables in classes

    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_bu ilder {
    // 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_bu ilder($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($w hat, $newinfo)
    {
    $this -> {'t_'.$what} = array_merge($th is -> {'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

    #2
    Re: Use of variables in classes

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

    --
    Tony Marston

    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



    "Michael" <letters@britis hlibrary.net> wrote in message
    news:c8d3lk$eu7 $1@hercules.bti nternet.com...[color=blue]
    > 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_bu ilder {
    > // 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_bu ilder($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($w hat, $newinfo)
    > {
    > $this -> {'t_'.$what} = array_merge($th is -> {'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;
    > }
    > }
    >
    >[/color]


    Comment

    • Chung Leong

      #3
      Re: Use of variables in classes

      "Michael" <letters@britis hlibrary.net> wrote in message
      news:c8d3lk$eu7 $1@hercules.bti nternet.com...[color=blue]
      > 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
      >[/color]

      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.


      Comment

      • Michael

        #4
        Re: Use of variables in classes

        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:HpednQBY75 qiFDfdRVn-gQ@comcast.com. ..[color=blue]
        > "Michael" <letters@britis hlibrary.net> wrote in message
        > news:c8d3lk$eu7 $1@hercules.bti nternet.com...[color=green]
        > > 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 -[/color]
        >[color=green]
        > > var_name and when I can just use $varname, and when it's necessary to[/color][/color]
        use[color=blue][color=green]
        > > 'var $varname' and when this can be left out.
        > > Here's the class - could it be made simpler?
        > > Michael
        > >[/color]
        >
        > 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[/color]
        writing.[color=blue]
        >
        >[/color]


        Comment

        • Chung Leong

          #5
          Re: Use of variables in classes

          "Michael" <letters@britis hlibrary.net> wrote in message
          news:c8h7ar$hh3 $1@sparta.btint ernet.com...[color=blue]
          > 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[/color]
          on[color=blue]
          > the page much easier to understand and therefore easier and quicker to
          > update.[/color]

          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.


          Comment

          • Michael

            #6
            Re: Use of variables in classes

            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:TJednU4Ir7 vDujHdRVn-gQ@comcast.com. ..[color=blue]
            > "Michael" <letters@britis hlibrary.net> wrote in message
            > news:c8h7ar$hh3 $1@sparta.btint ernet.com...[color=green]
            > > I have a form which allows people to search a database of tutorials by[/color][/color]
            any[color=blue][color=green]
            > > combination of location, tutor, subject or date.
            > > Using this class I can set a number of columns, tables and 'WHERE'[/color][/color]
            clauses[color=blue][color=green]
            > > 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[/color][/color]
            code[color=blue]
            > on[color=green]
            > > the page much easier to understand and therefore easier and quicker to
            > > update.[/color]
            >
            > 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.
            >
            >[/color]


            Comment

            • Michael

              #7
              Re: Use of variables in classes

              Dear Tony,
              Thanks - Useful advice.
              Michael

              "Tony Marston" <tony@NOSPAM.de mon.co.uk> wrote in message
              news:c8dd62$s7k $1$8300dec7@new s.demon.co.uk.. .[color=blue]
              > Take a look at http://www.tonymarston.co.uk/php-mys...seobjects.html
              > for some tips on this subject.
              >
              > --
              > Tony Marston
              >
              > http://www.tonymarston.net
              >
              >
              > "Michael" <letters@britis hlibrary.net> wrote in message
              > news:c8d3lk$eu7 $1@hercules.bti nternet.com...[color=green]
              > > 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 -[/color]
              >[color=green]
              > > var_name and when I can just use $varname, and when it's necessary to[/color][/color]
              use[color=blue][color=green]
              > > 'var $varname' and when this can be left out.
              > > Here's the class - could it be made simpler?
              > > Michael
              > >
              > > class select_query_bu ilder {
              > > // 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_bu ilder($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($w hat, $newinfo)
              > > {
              > > $this -> {'t_'.$what} = array_merge($th is -> {'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;
              > > }
              > > }
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Chung Leong

                #8
                Re: Use of variables in classes


                "Michael" <letters@britis hlibrary.net> wrote in message
                news:c8ic8m$2sq $1@sparta.btint ernet.com...[color=blue]
                > 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
                >[/color]

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


                Comment

                • Michael

                  #9
                  Re: Use of variables in classes

                  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_unitc ost,
                  prd.prd_forsale , dt.date_more_x, tl.tut_priority , tl.tut_more_x";

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

                  $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_eventdescript ion 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_priorit y
                  = 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';


                  Comment

                  • Chung Leong

                    #10
                    Re: Use of variables in classes

                    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_forsal e = 1";
                    $conds[] = ($t) ? "tl.tut_id = $t" : "tl.tut_priorit y = 1";
                    $conds[] = ($pi) ? "dsc.eve_id = ev.eve_id" : null;
                    ....
                    implode(" AND ", array_filter($c onds));

                    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@britis hlibrary.net> wrote in message
                    news:c8jhg2$epb $1@hercules.bti nternet.com...[color=blue]
                    > You may be right - it works for me though - I wouldn't use it if I was[/color]
                    just[color=blue]
                    > 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[/color]
                    for[color=blue]
                    > another client - in my opinion interspersing lots of conditional[/color]
                    statements[color=blue]
                    > 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[/color]
                    mean[color=blue]
                    > 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_unitc ost,
                    > prd.prd_forsale , dt.date_more_x, tl.tut_priority , tl.tut_more_x";
                    >
                    > if ($pi) {$query .= ', dsc.eve_descrip tion';}
                    >
                    > $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_eventdescript ion 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[/color]
                    =[color=blue]
                    > 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[/color]
                    dt.date_priorit y[color=blue]
                    > = 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';
                    >
                    >[/color]


                    Comment

                    • Michael

                      #11
                      Re: Use of variables in classes

                      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-dnW3mWsyOMzPd4p 2dnA@comcast.co m...[color=blue]
                      > And exactly how you wouldn't need if statement if you use the class? All[/color]
                      it[color=blue]
                      > does is implode the conditions together, which is perfectly doable without
                      > the use of a class.
                      >
                      > $conds[] = ($tpi) ? "prd.prd_id = $tpi" : "prd.prd_forsal e = 1";
                      > $conds[] = ($t) ? "tl.tut_id = $t" : "tl.tut_priorit y = 1";
                      > $conds[] = ($pi) ? "dsc.eve_id = ev.eve_id" : null;
                      > ...
                      > implode(" AND ", array_filter($c onds));
                      >
                      > 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[/color]
                      easy[color=blue]
                      > stuff. The complexity is in the logic of the query not its syntax.
                      >
                      >
                      > "Michael" <letters@britis hlibrary.net> wrote in message
                      > news:c8jhg2$epb $1@hercules.bti nternet.com...[color=green]
                      > > You may be right - it works for me though - I wouldn't use it if I was[/color]
                      > just[color=green]
                      > > 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[/color]
                      > for[color=green]
                      > > another client - in my opinion interspersing lots of conditional[/color]
                      > statements[color=green]
                      > > is a messy solution compared to the relative 'cleanness' of code that[/color][/color]
                      you[color=blue][color=green]
                      > > can get by using a class. I appreciate that using a class will actually[/color]
                      > mean[color=green]
                      > > 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[/color][/color]
                      the[color=blue][color=green]
                      > > 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,[/color][/color]
                      ctc.ctc_name,[color=blue][color=green]
                      > > v.ven_id, v.ven_town, v.ven_url, co.country_name , prc.price_unitc ost,
                      > > prd.prd_forsale , dt.date_more_x, tl.tut_priority , tl.tut_more_x";
                      > >
                      > > if ($pi) {$query .= ', dsc.eve_descrip tion';}
                      > >
                      > > $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_eventdescript ion dsc';}
                      > >
                      > > $query .= " WHERE prd.cat_id=1 AND prd.prd_id = ev.prd_id AND evd.eve_id[/color][/color]
                      =[color=blue][color=green]
                      > > ev.eve_id AND dt.prd_id = ev.prd_id AND tl.prd_id = ev.prd_id AND[/color][/color]
                      t.tut_id[color=blue]
                      > =[color=green]
                      > > tl.tut_id AND v.ven_id = ev.ven_id AND co.country_id = v.country_id[/color][/color]
                      AND[color=blue][color=green]
                      > > prc.price_id = prd.price_id AND ev.ctc_id = ctc.ctc_id AND[/color]
                      > dt.date_priorit y[color=green]
                      > > = 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';
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Chung Leong

                        #12
                        Re: Use of variables in classes

                        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@britis hlibrary.net> wrote in message
                        news:c8p2f4$shp $1@hercules.bti nternet.com...[color=blue]
                        > 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-dnW3mWsyOMzPd4p 2dnA@comcast.co m...[color=green]
                        > > And exactly how you wouldn't need if statement if you use the class? All[/color]
                        > it[color=green]
                        > > does is implode the conditions together, which is perfectly doable[/color][/color]
                        without[color=blue][color=green]
                        > > the use of a class.
                        > >
                        > > $conds[] = ($tpi) ? "prd.prd_id = $tpi" : "prd.prd_forsal e = 1";
                        > > $conds[] = ($t) ? "tl.tut_id = $t" : "tl.tut_priorit y = 1";
                        > > $conds[] = ($pi) ? "dsc.eve_id = ev.eve_id" : null;
                        > > ...
                        > > implode(" AND ", array_filter($c onds));
                        > >
                        > > 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[/color]
                        > easy[color=green]
                        > > stuff. The complexity is in the logic of the query not its syntax.
                        > >
                        > >
                        > > "Michael" <letters@britis hlibrary.net> wrote in message
                        > > news:c8jhg2$epb $1@hercules.bti nternet.com...[color=darkred]
                        > > > You may be right - it works for me though - I wouldn't use it if I was[/color]
                        > > just[color=darkred]
                        > > > building one query statement, but for a series of related statements,[/color][/color][/color]
                        it[color=blue][color=green][color=darkred]
                        > > > 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[/color][/color][/color]
                        solve[color=blue][color=green]
                        > > for[color=darkred]
                        > > > another client - in my opinion interspersing lots of conditional[/color]
                        > > statements[color=darkred]
                        > > > is a messy solution compared to the relative 'cleanness' of code that[/color][/color]
                        > you[color=green][color=darkred]
                        > > > can get by using a class. I appreciate that using a class will[/color][/color][/color]
                        actually[color=blue][color=green]
                        > > mean[color=darkred]
                        > > > 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[/color][/color]
                        > the[color=green][color=darkred]
                        > > > 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,[/color][/color]
                        > ctc.ctc_name,[color=green][color=darkred]
                        > > > v.ven_id, v.ven_town, v.ven_url, co.country_name , prc.price_unitc ost,
                        > > > prd.prd_forsale , dt.date_more_x, tl.tut_priority , tl.tut_more_x";
                        > > >
                        > > > if ($pi) {$query .= ', dsc.eve_descrip tion';}
                        > > >
                        > > > $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_eventdescript ion dsc';}
                        > > >
                        > > > $query .= " WHERE prd.cat_id=1 AND prd.prd_id = ev.prd_id AND[/color][/color][/color]
                        evd.eve_id[color=blue]
                        > =[color=green][color=darkred]
                        > > > ev.eve_id AND dt.prd_id = ev.prd_id AND tl.prd_id = ev.prd_id AND[/color][/color]
                        > t.tut_id[color=green]
                        > > =[color=darkred]
                        > > > tl.tut_id AND v.ven_id = ev.ven_id AND co.country_id = v.country_id[/color][/color]
                        > AND[color=green][color=darkred]
                        > > > prc.price_id = prd.price_id AND ev.ctc_id = ctc.ctc_id AND[/color]
                        > > dt.date_priorit y[color=darkred]
                        > > > = 1 ";
                        > > >
                        > > > if ((!$isildur) && (!$tpi)) { $query.=' AND UNIX_TIMESTAMP( date_end)[/color][/color]
                        >[color=green][color=darkred]
                        > > > (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';
                        > > >
                        > > >[/color]
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Michael

                          #13
                          Re: Use of variables in classes

                          > Doing with variables in classes is fairly simple: you have to use $this->[color=blue]
                          > all the time.[/color]

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


                          Comment

                          • Tony Marston

                            #14
                            Re: Use of variables in classes

                            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
                            This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



                            "Michael" <letters@britis hlibrary.net> wrote in message
                            news:c8ra93$kbp $1@sparta.btint ernet.com...[color=blue][color=green]
                            > > Doing with variables in classes is fairly simple: you have to use[/color][/color]
                            $this->[color=blue][color=green]
                            > > all the time.[/color]
                            >
                            > That's one of the parts I'm confused about - clearly there are a lot of
                            > instances
                            > where variables are refered to just using $varname rather than $this ->
                            > varname
                            > within classes.
                            > Michael
                            >
                            >[/color]


                            Comment

                            • Michael

                              #15
                              Re: Use of variables in classes

                              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.de mon.co.uk> wrote in message
                              news:c8shkg$k6l $1$8300dec7@new s.demon.co.uk.. .[color=blue]
                              > 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@britis hlibrary.net> wrote in message
                              > news:c8ra93$kbp $1@sparta.btint ernet.com...[color=green][color=darkred]
                              > > > Doing with variables in classes is fairly simple: you have to use[/color][/color]
                              > $this->[color=green][color=darkred]
                              > > > all the time.[/color]
                              > >
                              > > That's one of the parts I'm confused about - clearly there are a lot of
                              > > instances
                              > > where variables are refered to just using $varname rather than $this ->
                              > > varname
                              > > within classes.
                              > > Michael
                              > >
                              > >[/color]
                              >
                              >[/color]


                              Comment

                              Working...