Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
php-oop/index.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
132 lines (119 sloc)
3.5 KB
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title> Introduction to Object-Oriented Programming </title> | |
<link type='text/css' rel='stylesheet' href='css/main.css'/> | |
</head> | |
<body> | |
<p> | |
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
?> | |
<?php | |
// 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(); | |
?> | |
</p> | |
<p> | |
<!-- Your code here --> | |
<?php | |
class People { | |
public $isAlive = true; | |
public $firstname; | |
public $lastname; | |
public $age; | |
} | |
$teacher = new People(); | |
$student = new People(); | |
echo $teacher->isAlive; | |
?> | |
</p> | |
<p> | |
<!-- Your code here --> | |
<?php | |
class Colleague { | |
public $isAlive = true; | |
public $firstname; | |
public $lastname; | |
public $age; | |
public function __construct($firstname, $lastname, $age) { | |
$this->firstname = $firstname; | |
$this->lastname = $lastname; | |
$this->age = $age; | |
} | |
public function greet() { | |
return "Nice to meet you!"; | |
} | |
} | |
$teacher = new Colleague("Joel", "Salisbury", "Old"); | |
$student = new Colleague("Brandon", "Nickle", 25); | |
echo $teacher->greet(); | |
echo $student->greet(); | |
?> | |
</p> | |
<p> | |
<!-- Your code here --> | |
<?php | |
class Dog { | |
public $numLegs = 4; | |
public $name; | |
public function__construct($name) { | |
$this->name = $name; | |
} | |
public function bark() { | |
return "Woof!"; | |
} | |
public function greet() { | |
return "I am" . $this->name; | |
} | |
$dog1 = new Dog("Barker"); | |
$dog2 = new Dog("Amigo"); | |
echo $dog1->bark(); | |
echo $dog2->greet(); | |
} | |
?> | |
</p> | |
<p> | |
<!-- Your code here --> | |
<?php | |
class Cat { | |
public $isAlive = true; | |
public $numLegs = 4; | |
public $name; | |
public function__construct($name) { | |
$this->name = $name; | |
} | |
public function meow() { | |
return "Meow meow"; | |
} | |
public function greet() { | |
return "I am" . $this->name; | |
} | |
$cat1 = new Cat("CodeCat"); | |
echo $cat1->meow(); | |
} | |
?> | |
</p> | |
</body> | |
</html> |