Created
April 17, 2013 18:28
-
-
Save loraxx753/5406574 to your computer and use it in GitHub Desktop.
Intro to PHP classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This class defines what a dog is | |
class Dog { | |
// What runs when you make a "new Dog()" | |
function __construct($breed, $size) { | |
// Doing $this->whatever = $whatever assigns whatever's | |
// in the parenthesis to THAT particular dog | |
// that's being defined. | |
$this->breed = $breed; | |
$this->size = $size; | |
} | |
// What exactly does a bark do? | |
public function bark() { | |
// If the dog is small, make a small dog noise. | |
if($this->size == 'small') | |
{ | |
echo "yip!"; | |
} | |
// If the dog is big, bark like a big dog. | |
else if($this->size == 'big') | |
{ | |
echo "WOOF!"; | |
} | |
} | |
} | |
// New dog named Rex, who's a pitbull and is big | |
$rex = new Dog("Pitbull", "big"); | |
// A different dog named fido, who's a poodle and is small | |
$fido = new Dog("Poodle", "small"); | |
$rex->bark(); // Will echo WOOF! | |
$fido->bark(); // Will echo yip! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment