Skip to content

Instantly share code, notes, and snippets.

@mogetutu
Created February 17, 2014 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogetutu/9047576 to your computer and use it in GitHub Desktop.
Save mogetutu/9047576 to your computer and use it in GitHub Desktop.
OOP - Class Person
<?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