Re: Extending a Class

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

 



Brent Baisley wrote:
You're absolutely correct. I was debating on whether to get into inheritance and overloading. I just settled on what PEAR tends to use rather than go into more detail.

Are you referring to the PEAR::isError (and similar function calls) that you see all over the place in PEAR code? If this is what you meant, then let me add something. Some PEAR classes don't require *any* object to exist. These are what you call "static" classes and "static" methods.


When a class function is called statically (i.e. PEAR::isError) then it is pretty much like having a normal function called PEAR_isError(). There is no object and no properties except for static properties; and these static properties are variables that are tied to a class instead of an object.

The main benefits for PEAR developers doing things this way is so that they don't have to worry about name collisions and they can share certain variables between these "static" functions.

I'm starting to get off track (what else is new? :) but if you like I can explain static variables a bit more as well.


Thanks for clarifying.

Long story short: inherit methods from parents. Use those methods, unless there is a good reason not to use one. If there is, then you override it in the child. If you must use a *static* method from a class, then use the :: operator.




On Jan 21, 2005, at 11:18 AM, Matthew Weier O'Phinney wrote:

* 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




--
Teach a man to fish...

NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://www.php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php
LAZY | http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins


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