objects and classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

objects and classes

Post by shiznatix »

i have a few questions about classes and objects. first of all what is a object? also what does $this->somthing do and were does "$this" and "somthing" come from? i dont think i quite understand the concept of classes maybe i can get a good understanding of the very basics of classes?
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Code: Select all

$this ->something
"$this" is an instance of an object and "->" allows you to access properties, methods etc of that object class. So "something" could be an object method or variable.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I'll give it my best shot [$i->giveBestShot();]

So basically an object is a ball that you are holding in your hand. There are methods that you are able to use in terms of this ball. For example, ball->getColor() may return the color of the ball. The ball has a field within it, called color which stores the value 'red.'

Lets look at this programmatically:

Code: Select all

class Ball {

  var color;

  function Ball ( $col ) {
    $this->color = $col;
  }

  function getColor ( ) {
    return $this->color;
  }

}

  $b1 = new Ball ( "Blue" );
  $b2 = new Ball ( "Red" );

  echo "Ball 1's color: ".$b1->getColor();
  echo "<br>Ball 2's color: ".$b2->getColor();
"$this->" is used within the ball, to refer to the object's own variables.

Hope that was a little bit helpful :?
Post Reply