Created
February 17, 2014 09:36
-
-
Save mogetutu/9047576 to your computer and use it in GitHub Desktop.
OOP - Class Person
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 | |
class Person | |
{ | |
public $name; | |
public $age; | |
public $favourite_food; | |
function __construct($name = "Name not defined", $age = "Age not defined", $favourite_food = "No Favourite Food") | |
{ | |
$this->name = $name; | |
$this->age = $age; | |
$this->favourite_food = $favourite_food; | |
} | |
function name() | |
{ | |
return $this->name; | |
} | |
function age() | |
{ | |
return $this->age; | |
} | |
function favourite_food() | |
{ | |
return $this->favourite_food; | |
} | |
function about() | |
{ | |
$name = $this->name(); | |
$age = $this->age(); | |
$favourite_food = $this->favourite_food(); | |
return "My name is $name, I'm $age yrs old and I love $favourite_food"; | |
} | |
} | |
// GET request | |
$person1 = new Person( | |
$_GET['name'], | |
$_GET['age'], | |
$_GET['food']); | |
var_dump($person1->about()); | |
// POST request | |
if($_POST) | |
{ | |
$person2 = new Person( | |
$_POST['name'], | |
$_POST['age'], | |
$_POST['food'] | |
); | |
var_dump($person2->about()); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Form</title> | |
</head> | |
<body> | |
<form action="" method="POST"> | |
<input type="text" name="name" placeholder="name"> | |
<input type="text" name="age" placeholder="age"> | |
<input type="text" name="food" placeholder="food"> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment