Good Afternoon,
I went through the killerphp tutorial for Object Oriented PHP and I understood much of the basics and concepts applied by going through the example myself.
My main concern (as per the below code) is that I want to add a getter method "get_person()" to my class that will return all the properties of any object (I may have missed/misunderstood some points of the tutorial).
Example:
Each object property has a defined setter method, and another setter method is defined to collect those methods altogether "set_person($firstname,$lastname,$age,$birthdate,$weight,$height)".
I was wondering if I can construct a getter method to return all object properties in an array (for instance) and how can I read those properties in my "index.php" file.
"class_lib.php"
<?php
class person
{
/*Properties*/
var $firstname;
var $lastname;
var $age;
var $birthdate;
var $weight;
public $height;
var $person_property= array();
protected $social_insurance;
private $pinn_number;
/*Constructor*/
function __construct($firstname,$lastname,$age,$birthdate,$weight,$height)
{
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
$this->birthdate = $birthdate;
$this->weight = $weight;
$this->height = $height;
}
/*Methods*/
/*Setters*/
function set_firstname($firstname)
{
$this->firstname=$firstname;
}
function set_lastname($lastname)
{
$this->lastname=$lastname;
}
function set_age($age)
{
$this->age=$age;
}
function set_birthdate($birthdate)
{
$this->birthdate=$birthdate;
}
function set_weight($weight)
{
$this->weight=$weight;
}
function set_height($height)
{
$this->height=$height;
}
function set_person($firstname,$lastname,$age,$birthdate,$weight,$height)
{
$this->firstname=$firstname;
$this->lastname=$lastname;
$this->age=$age;
$this->birthdate=$birthdate;
$this->weight=$weight;
$this->height=$height;
}
/*Getters*/
function get_firstname()
{
return $this->firstname;
}
function get_lastname()
{
return $this->lastname;
}
function get_age()
{
return $this->age;
}
function get_birthdate()
{
return $this->birthdate;
}
function get_weight()
{
return $this->weight;
}
function get_height()
{
return $this->height;
}
/*********************************************************************/
function get_person()
{
$this->get_firstname();
$this->get_lastname();
$this->get_age();
$this->get_weight();
$this->get_height();
}
/*********************************************************************/
}
?>
"index.php"
<?php
include("class_lib.php");
$yhammad = new person("Yehia","Hammad",25,"25-05-1986",85,192);
echo $yhammad->firstname ." ". $yhammad->lastname .
" born on " . $yhammad->birthdate .
" is " . $yhammad->age .
" years old with a height and weight of " . $yhammad->height .
"cm and " . $yhammad->weight . "kg<br />";
$yhammad->set_person("Yehia","Hammad",26,"16-04-1986",80,192);
echo $yhammad->firstname ." ". $yhammad->lastname .
" born on " . $yhammad->birthdate .
" is " . $yhammad->age .
" years old with a measured height and weight of " . $yhammad->height .
"cm and " . $yhammad->weight . "kg";
?>