Jump to content

what is a class for??


M.O.S. Studios

Recommended Posts

a class is like an object, that is replicatable

 

for example:

 

class Cat {

  var $name;

  function getName() { return $this->name; }

}

$cat1 = new Cat();

$cat1->name = 'Tabby';

 

$cat2 = new Cat();

$cat2->name = "Bob";

 

$cat1->getName()." loves talking to ".$cat2->getName()."!";

 

all it does is allow you to give multiple things the same proceedures, but you can make every single instance of those proceedures,functions,variables, unique to that specific instance

Link to comment
Share on other sites

a class is like an object, that is replicatable

 

no not really... a class is like a blueprint/design/description for an object.

 

Like for instance, if you want to define a dog.

 

Things, or 'properties' of a dog:

 

- name

- size

- weight

- color

- breed

 

Things, or 'methods' a typical dog might do/have:

 

- eat

- poop

- sleep

 

So these things about a dog would be described in a class, and for example, your little dog muffy would be an instance of that dog class.

 

Link to comment
Share on other sites

ok.. look @ it this way..

 

pretend your php application had a party lol.. and they were counting guests..

 

<?php
class Guest {
public $firstName, $lastName, $carColor, $carMake, $age;
public function __construct($fn,$ln,$cc,$cm,$a) {
	$this->firstName = $fn;
	$this->lastName = $ln;
	$this->carColor = $cc;
	$this->carMake = $cm;
	$this->age = $a;
}
}
$guests = array();
$guests[] = new Guest('Billy','Joel','blue','Audi',19);
$guests[] = new Guest('Billy','Joel','black','Mercedes',29);
$guests[] = new Guest('Darnel','Jones','red','Ford',14);
$guests[] = new Guest('Wayne','Taylor','silver','Lincoln',24);
foreach ($guests as $k => $guest) {
echo 'Guest '.$k.' is '.$guest->age.' with a '.$guest->carColor.' '.$guest->carMake.'!?!?!?! HOW??<br />';
}
?>

Link to comment
Share on other sites

is a fancy name for a collection of functions, with an output thats changeable depending on what you feed it

 

Kinda. Typically a class will be an organized structure of methods (functions), properties (variables), and other objects to perform a specific task or a range of tasks.

 

I added a bit to the Guest class:

<?php

// Our Guest Class:
class Guest {
// Setup the properties of the class:
public $firstName, $lastName, $carColor, $carMake, $age, $cups, $bags;

// Add what happens when you create a new guest
public function __construct($fn,$ln,$cc,$cm,$a) {
	// Set their properties
	$this->firstName = $fn;		// Get this from the passed arguments
	$this->lastName = $ln;		// Get this from the passed arguments
	$this->carColor = $cc;		// Get this from the passed arguments
	$this->carMake = $cm;		// Get this from the passed arguments
	$this->age = $a;		// Get this from the passed arguments
	$this->cups = 0;		// Set this to 0 as default
	$this->bags = 0;		// Set this to 0 as default
}

// What if our guests get thirsty?
public function drankSoda($cups) {
	// We should keep track of how many sodas they have
	$this->cups = $this->cups + $cups;
}

// But then they might get hungry too!
public function ateChips($bags) {
	// So let's keep tally of who eats the most at my party
	$this->bags = $this->bags + $bags;
}

}

// We normally would create an object like this:
$Phil = new Guest('Phil', 'IsCool', 'green', 'Mini', 19);

// Then, we could say I drank 2 cups of soda:
$Phil->drankSoda(2);

// I then got super hungry and had 5 bags of chips:
$Phil->ateChips(5);

// Finally, I had to wash those chips down and had one more cup of soda:
$Phil->drankSoda(1);

// In the end, let's see how I did:
echo $Phil->firstName.' '.$Phil->lastName.' drove up in a '.$Phil->carColor.' '.$Phil->carMake.' and had '.$Phil->cups.' cups of soda and '.$Phil->bags.' bags of chips!';

// That would show on screen:
// Phil IsCool drove up in a green Mini and had 3 cups of soda and 5 bags of chips.
?>

 

Maybe that will make more sense?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.