Re: Classes and Functions

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

 



On Sat, 2004-12-11 at 13:03 -0500, R. Van Tassel wrote:
> Can someone give me a distinction between the two and when to use / not use
> them?
> 
>  
> 
> I want to thank everyone who replied about my"For" loop question. All the
> answers were VERY helpful! Thanks very much.

You might want to pick up a book on programming to read up on this. 

In a nutshell, a function should do something specific. It's good to use
functions when you do the same thing in different places in your code so
that you don't need to copy/paste your code over and over.

function foo()
{
    $x = 1;
    $y = 2;
    return $x + $y;
}

$bar = foo();

print $bar;
-----
OUTPUT:
3

A class/object is a collection of functions and variables that are
contained within their own scope. (vague description)

class foobar
{
  var $x = 10;

  var $y = NULL;

  function foobar($y)
  {
     $this->y = $y;
  }

  function add()
  {
    return $this->x + $this->y;
  }

  function subtract()
  {
    return $this->x - $this->y;
  }

}

$object =& new foobar(4);

print $object->add() . "\n";
print $object->subtract() . "\n";

# set y to a new number
$object->y = 2;

print $object->add() . "\n";
print $object->subtract() . "\n";

--------
OUTPUT:
> 14
> 6
> 12
> 8

As it sounds like you're still new to this, I would pick up a book or
read some material online that will better show you when to use either
and play around with them both. 

Have fun

-Robby

-- 
/***************************************
* Robby Russell | Owner.Developer.Geek
* PLANET ARGON  | www.planetargon.com
* Portland, OR  | robby@xxxxxxxxxxxxxxx
* 503.351.4730  | blog.planetargon.com
* PHP/PostgreSQL Hosting & Development
*    --- Now supporting PHP5 ---
****************************************/

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