Categories
php unit Samuel

Unit 9 Object Oriented Programming, Part 1

Unit 1 and 2 are review

// The code below creates the class
class Person {
// Creating some properties (variables tied to an object)
public $isAlive = true;
public $firstname;
public $lastname;
public $age;

// Assigning the values
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}

// Creating a method (function tied to an object)
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
}

// Creating a new person called "boring 12345", who is 12345 years old ;-)
$me = new Person('boring', '12345', 12345);

// Printing out, what the greet method returns
echo $me->greet();

Unit 3:

class Person{

}
$teacher = new Person();
$student = new Person();

Unit 4:


class Person{
public $isAlive = true;
public $firstname; //don't assign values to these in the public class, define them locally
public $lastname;
public $age;

}

$teacher = new Person();
$student = new Person();

//print out info
echo $teacher->isAlive; //note don't add a $to the isAlive value here it will cause syntax error

?>

unit 5:

class Person{
public $isAlive = true;
public $firstname; //don't assign values to these in the public class, define them locally
public $lastname;
public $age;

//assign values
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;

}

}

$teacher = new Person("boring", "12345", 12345);
$student = new Person("sam", "bell", 28);

//print out info
echo $teacher->isAlive;
echo $student->age;


unit 8:

class Dog {
public $numLegs = 4;
public $name;

public function __construct ($name){
$this->name = $name;

}

public function greet(){
return " Hello, my name is " . $this->name . ". nice to meet you";

}

public function bark(){
return "Woof!";
}

}

$dog1 = new Dog ("Barker");
$dog2 = new Dog ("Amigo");

echo $dog1->bark();
echo $dog2->greet();

?>

unit 9:


// Your code here
class Cat {

public $isAlive = true;
public $numLegs = 4;
public $name;
}

public function __construct ($name){
$this->name = $name;

}

public function meow(){
return "Meow meow";
}

$cat1 = new Cat ("CodeCat");

echo $cat1->meow();

}