Re: Extending a Class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Brent Baisley <brent@xxxxxxxxxxxx> :
> You've got most of it. Just adding "extends MySQL" to your page result 
> class will allow you to access all function in the MySQL class. I 
> usually reference functions in the parent class like this:
> parent::FunctionName()

This is a bad practice when inheriting, usually -- the whole point is
that the parent classes methods are directly available to the child
class without using such constructs. 

    $this-> FunctionName()

will execute the method 'FunctionName' from the current class -- or any
parent class if it is not defined in the current class. There's one good
exception:

> You still reference functions in the current class using $this-> , that 
> way you can have functions of the same name in both classes.

To make a method in the child class of the same name as a method in the
parent class is called overriding -- and is usually done to either
change the behaviour of that method (for example, if you want to do
something completely different than what was done in the parent class)
or to supplement the behaviour of the parent method -- for instance, if
the child class wishes to do some processing before or after the parent
method is executed. In this latter case, you *will* use the
parent::FunctionName() construct -- so that you can actually access the
method you're currently overriding:
    
    class parentClass 
    {
        // ...

        function someMethod() 
        {
            // do something
        }
    }

    class someClass extends parentClass 
    {
        // ...

        function someMethod() 
        {
            // do some processing first
            parent::someMethod();
            // do some postprocessing
        }
    }

So, in the OP's case, he might want to have override the fetchAssoc()
method in his PageResultSet subclass so that it tacks on the LIMIT info
to the SQL statement, and then have it call the parent (Mysql class)
fetchAssoc() method with that modified SQL.

> On Jan 20, 2005, at 9:17 PM, Phillip S. Baker wrote:
>
> > Greetings all,
> >
> > I have a class I use for MySQL connection and functions.
> >
> > I also have a class that I use to create Paged Results.
> >
> > The paged results class connects to a DB and uses allot of sql calls
> > to make this happen. I am noticing that it does allot that the MySQL
> > class does.
> >
> > What I would like to do is to Have the paged results extend the
> > MySQL class so it can use the functions within that class so I can
> > keep them updated in just one place. How would I go about doing
> > that?? Is it as simple as something like this (this is shortened for
> > convience)??
> >
> > class Mysql {
> >  var $results;
> >  var $dbcnx;
> >
> >  function Mysql($query, $cnx) { // Constructor function
> >    $this-> dbcnx = $cnx;
> >    if (!$this-> results = @mysql_query($query))
> >     $this-> _error("There was an error with executing your query. Try 
> > again
> > later.", $query);
> >  }
> > }
> >
> > class PageResultSet extends MySQL {
> >  var $results;
> >
> >  function PageResultSet ($query,$pageSize,$resultpage,$cnx) {
> >
> >   $this-> results = @mysql_query($query,$cnx) or $this-> _error('Error 
> > Running
> > your search. Try back later.', $query);
> >   $this-> pageSize = $pageSize;
> >   if ((int)$resultpage <= 0) $resultpage = 1;
> >   if ($resultpage > $this-> getNumPages())
> >   $resultpage = $this-> getNumPages();
> >   $this-> setPageNum($resultpage);
> >  }
> > }
> >
> > I would like to be able to pass the results from the MySQL class to the
> > PageResultSet class without have to do the query over and such.
> > How would I go about coding that? I am not clear on that.
> >
> > Also can I extend a function in PageResultSet that is started in 
> > MySQL??
> > In MySQL I have
> >  function fetchAssoc() {
> >   if (!$this-> results) return FALSE;
> >   $this-> row++;
> >   return mysql_fetch_assoc($this-> results);
> >  }
> >
> > In PageResultSet I have
> >  function fetchAssoc() {
> >   if (!$this-> results) return FALSE;
> >   if ($this-> row > = $this-> pageSize) return FALSE;
> >   $this-> row++;
> >   return mysql_fetch_assoc($this-> results);
> >  }
> >
> > Can I just write something like within PageResultSet
> > function fetchAssocPRS extends fetchAssoc () {
> >   if ($this-> row > = $this-> pageSize) return FALSE;
> > }
> >
> > Thanks for the help.
> >
> > --
> > Blessed Be
> >
> > Phillip
> >
> > -- 
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >


-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux