Jump to content

Return multiple object properties in a single method


yhammad

Recommended Posts

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";
?>

Link to comment
Share on other sites

  • 1 year later...

Huh, I must have missed it when this was originally posted. Yes, the correct solution is to return an array, with each of those items in an array. You might consider using an associative array to make your code a little more readable when you use the returned array. That way, you can access the array using

 

$mydata['name']

 

rather than

 

$mydata[1]

 

And constantly having to remember what each spot in the array is for. In that case, you'd use:

 

function get_person()

{

$r['firstname'] = $this->get_firstname();

$r['lastname'] = $this->get_lastname();

$r['age'] = $this->get_age();

$r['weight'] = $this->get_weight();

$r['height'] = $this->get_height();

return $r;

}

 

Keep in mind, there's little point in using the getters from within the object, so you may as well just use "$this->firstname" rather than "$this->get_firstname()" and make it slightly easier on PHP. The getters are there to access your object's properties from "outside" the object, so you can control how they are accessed or used.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...