Family Tree in Object Oriented PHP Problem Solvi...


Welcome, Guest. Please login or register.
Username:   Password:

Hosted in the Cloud RSS

Recent Topics

Gallery Latest


752 Views
0 comment(s)

732 Views
0 comment(s)


1084 Views
0 comment(s)

928 Views
0 comment(s)


810 Views
0 comment(s)

836 Views
1 comment(s)

Search The Gallery

User Info

Welcome, Guest. Please login or register.
September 25, 2017, 18:08:06 PM

Login with username, password and session length

Forum
Web
Gallery
Here you can post questions and get answers to help you solve problems with Windows based PC’s

We welcome your questions, with over 15 years experience with PC’s we might even find the answer for you.

   

DatahopaForumTechnology Chat Problem SolvingFamily Tree in Object Oriented PHP



Pages: [1]

Family Tree in Object Oriented PHP

Viewing: 0 Members and 1 Guest       Total Views: 4386

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

Let's build a family tree in Object Oriented PHP.  To keep the source code listing short and easy to read, we will simplify and intentionally code both grandparents to have the same eye and hair color, and both parents to have the same eye and hair color.  

Code:
<?php

class grandparents 
{
    public function 
__construct(){
        
$this->inherit();
    }

    public function 
inherit(){
        
$this->eyes 'blue';
        
$this->hair 'blond';
    }
}

class 
parents extends grandparents
{
    public function 
__construct()
    {
        
grandparents::__construct();
        
$this->eyes 'green';
        
$this->hair 'redhead';
    }

}
class 
children extends parents
{
    public function 
__construct()
    {
        
$this->inherit();
    }
}

$grandparents = new grandparents();
$parents = new parents();
$children = new children();
$familytree=array(
array("Grandparents eye color: ",$grandparents->eyes),
array("Grandparents hair color: ",$grandparents->hair),
array("Parents eye color: ",$parents->eyes),
array("Parents hair color: ",$parents->hair),
array("Children eye color: ",$children->eyes),
array("Children hair color: ",$children->hair),
);

echo 
var_export($familytree,TRUE);

?>


I really enjoy using the syntax highlighting of this site.  Here's the output as a multidimensional array:

Code:

array (
  0 =>
  array (
    0 => 'Grandparents eye color: ',
    1 => 'blue',
  ),
  1 =>
  array (
    0 => 'Grandparents hair color: ',
    1 => 'blond',
  ),
  2 =>
  array (
    0 => 'Parents eye color: ',
    1 => 'green',
  ),
  3 =>
  array (
    0 => 'Parents hair color: ',
    1 => 'redhead',
  ),
  4 =>
  array (
    0 => 'Children eye color: ',
    1 => 'blue',
  ),
  5 =>
  array (
    0 => 'Children hair color: ',
    1 => 'blond',
  ),
)


Who do you think the children will inherit from, the parents or grandparents?  
Scroll down on the output to find out.
Posted January 09, 2016, 02:42:31 AM Logged
My Very Enormous Monster Just Stopped Using Nine

Offline Data
United Kingdom Administrator
Posts: 4668
Rank: Certified

I really enjoy using the syntax highlighting of this site.  Here's the output as a multidimensional array:

Yes it's good isn't it, it's one of the reasons I use this forum software.
 

I haven't had time to solve this little puzzle yet, maybe someone else will beat me to it.
Posted January 10, 2016, 13:47:25 PM Logged

Offline DaveMorton
United States WWW Global Moderator
Posts: 2528
Rank: Certified
Comfort the Disturbed! Disturb the Comfortable!

Well, given that the only inherit() method is in the grandparents class, they would inherit from them, rather than the parents class. However, with an additional inherit() method in the parents class, the children could inherit their traits from the parents, instead. If I had more time, I would love to play with this a bit more, and add provisions for multiple parents/grandparents, possibly even introducing recursion/regression to allow for multiple previous generations, and possibly even dominant/recessive traits that more accurately model genetics, but that's going to have to be a story for another time, I'm afraid.
Posted January 10, 2016, 15:13:48 PM Logged
Safe, Reliable Insanity, Since 1961!
Chat With Morti!

CAPTCHA4us

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

Yes, Dave... That's correct.  And, that's a terrific idea too.

Data, scrolling down on the output box (the 2nd one), at the bottom we see:

Code:
  4 =>
  array (
    0 => 'Children eye color: ',
    1 => 'blue',
  ),
  5 =>
  array (
    0 => 'Children hair color: ',
    1 => 'blond',
  ),

This shows the children take after their grandparents, at the top of that output box:

Code:
 0 =>
  array (
    0 => 'Grandparents eye color: ',
    1 => 'blue',
  ),
  1 =>
  array (
    0 => 'Grandparents hair color: ',
    1 => 'blond',
  ),

Posted January 13, 2016, 01:15:12 AM Logged
My Very Enormous Monster Just Stopped Using Nine

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

Hey Dave,

Since you're busy at the moment, what do you say I recurse a little genealogy for our discussion purposes?

Code:
<?php

function recurse($genealogy){
    foreach(
$genealogy as $key => $record){
        if(
is_array($record)){
            
recurse($record);
        } else{

            echo 
"$record, ";
        }
    }
}


$familyline = array(
     
"grandfather"
     
"grandmother",
     array(
           
"father",
           
"mother",
           array(
                 
"son",
                 
"daughter",
                )
          )
     );


echo 
"Family line: ";
recurse($familyline);


?>


Later, we can port this listing to object oriented PHP.  For now, here's my first test run output.


Code:
Family line: grandfather, grandmother, father, mother, son, daughter,

How's my recursion doing Dave?  Can we optimize it, do you think?

Attention to all you genealogists out there... This may have been simplified to appeal to a wider audience, but even so, a three dimensional array is something not inconsequential, and deserves your attention.
Posted January 13, 2016, 01:43:33 AM
Last Edit: January 13, 2016, 02:06:45 AM by 8pla.net
Logged
My Very Enormous Monster Just Stopped Using Nine

Offline DaveMorton
United States WWW Global Moderator
Posts: 2528
Rank: Certified
Comfort the Disturbed! Disturb the Comfortable!

Well, that's one way to do it. Cheesy

I probably would have used a combo of regression and recursion for that, but regression is a very complicated thing, so perhaps to start, your method may be best.
Posted January 13, 2016, 07:22:12 AM Logged
Safe, Reliable Insanity, Since 1961!
Chat With Morti!

CAPTCHA4us

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

Let's do something illegal...

Code:
$related=array(
              array($familyline[0][0],$familyline[0][1]) => $familyline[1][0]
              );

/* PHP Warning:  Illegal offset type */




Our little genealogy script does not show which of the parents is related to the grandparents.

We've been warned by the PHP Police  Shocked.   Say regression sounds legal.  
Posted January 13, 2016, 13:37:32 PM Logged
My Very Enormous Monster Just Stopped Using Nine
Pages: [1]
Jump to:  

Site Activity
Most Online Today: 174
50 Guests, 1 User
Urania

fast-registration