NYCPHP Meetup

NYPHP.org

[nycphp-talk] OOP Newbie Building arrays in extended classes

Rob Marscher rmarscher at beaffinitive.com
Fri Jan 25 17:57:12 EST 2013


Here's where the info is for that in the php docs:
http://php.net/manual/en/language.oop5.decon.php

On Jan 25, 2013, at 5:51 PM, Jeff Slutz <jeff at jeffslutz.com> wrote:

> I think you're looking for parent::__construct();
> 
> The child's __construct() will replace the parent's.  So if you want to run the parent's you have to call it explicitly.
> 
> JS
> 
> --
> Jeff Slutz 
> JSLEUTH LLC
> 3242 44th ST APT 3F
> Astoria, NY 11103
> c. 970.443.9390
> jeff at jeffslutz.com
> 
> 
> On Fri, Jan 25, 2013 at 5:47 PM, Leam Hall <leamhall at gmail.com> wrote:
>> Okay, OOP newbie time again. In line 11 I declare an array. Per IRC conversations a day or so ago, line 49 has to go into the __construct function. However, the hope is that line 83 would add to what was done in line 49. It seems not to.
>> 
>> I think because the extends doesn't actually call the __construct. Is my understanding correct? How would I achieve the goal of adding line 83 so that $roles included 'troop' and 'nco' for an object of class NCO?
>> 
>> Thanks!
>> 
>> Leam
>> 
>> ####
>> 
>>    1 <?php
>>    2
>>    3 require_once 'stat_array.php';
>>    4
>>    5 class Person {
>>    6
>>    7     public  $name;
>>    8     public  $age;
>>    9     public $gender = "Female";
>>   10     public $skills = array();
>>   11     public $roles = array();
>>   12
>>   13     public function __construct() {
>>   14         $this->age = 10 + rand(1,7);
>>   15         $this->gender = $this->roll_gender(50);
>>   16         return true;
>>   17     }
>>   18
>>   19     protected function roll_age($min, $max) {
>>   20         return 17 + rand($min, $max);
>>   21     }
>>   22
>>   23     protected function roll_gender($trigger) {
>>   24         if (rand(1, 100) < $trigger) {
>>   25             return "Male";
>>   26         } else {
>>   27             return "Female";
>>   28         }
>>   29     }
>>   30
>>   31     protected function add_skill(&$skill_array, $skill) {
>>   32     // Note the array pass by reference, per ##php fluffypony
>>   33         if (array_key_exists($skill, $skill_array)) {
>>   34             $skill_array[$skill] = $skill_array[$skill] + 1;
>>   35         } else {
>>   36             $skill_array[$skill] = 1;
>>   37         }
>>   38         return $skill_array;
>>   39     }
>>   40 }
>>   41
>>   42 class Trooper extends Person {
>>   43
>>   44     private $rank_min = 1;
>>   45     private $rank_max = 2;
>>   46     protected $rank_class = 'rank_enlisted';
>>   47     public $rank;
>>   48     public function __construct($params) {
>>   49         $this->roles[] = 'troop';
>>   50         $this->age = $this->roll_age($this->rank_min, 3 + $this->rank_max);
>>   51         $this->gender = $this->roll_gender($params['percent_male']);
>>   52         $this->rank = $params[$this->rank_class][$this->get_rank($this->rank_min, $this->rank_max)];
>>   53         foreach($this->roles as $role) {
>>   54               $this->add_skill($this->skills, $params['base_skill'][$role]);
>>   55         }
>>   56
>>   57
>>   58         return true;
>>   59     }
>>   60
>>   61     protected function get_rank($rank_min, $rank_max) {
>>   62         return rand($rank_min, $rank_max);
>>   63     }
>>   64
>>   65 }
>>   66
>>   67 class Corporal extends Trooper {
>>   68     private $rank_min = 3;
>>   69     private $rank_max = 4;
>>   70
>>   71     public function __construct($params) {
>>   72         $this->age = $this->roll_age($this->rank_min , 5 + $this->rank_max);
>>   73         $this->gender = $this->roll_gender($params['percent_male']);
>>   74         $this->rank = $params[$this->rank_class][$this->get_rank($this->rank_min, $this->rank_max)];
>>   75     }
>>   76 }
>>   77
>>   78 class NCO extends Corporal {
>>   79     private $rank_min = 4;
>>   80     private $rank_max = 6;
>>   81
>>   82     public function __construct($params) {
>>   83         $this->roles[] = 'nco';
>>   84         $this->age = $this->roll_age($this->rank_min , 7 + $this->rank_max);
>>   85         $this->gender = $this->roll_gender($params['percent_male']);
>>   86         $this->rank = $params[$this->rank_class][$this->get_rank($this->rank_min, $this->rank_max)];
>>   87         foreach($this->roles as $role) {
>>   88               $this->add_skill($this->skills, $params['base_skill'][$role]);
>>   89         }
>>   90
>>   91     }
>>   92 }
>>   93
>>   94 class SNCO extends NCO {
>>   95     private $rank_min = 6;
>>   96     private $rank_max = 9;
>>   97
>>   98     public function __construct($params) {
>>   99         $this->age = $this->roll_age($this->rank_min , 10 + $this->rank_max);
>>  100         $this->gender = $this->roll_gender($params['percent_male']);
>>  101         $this->rank = $params[$this->rank_class][$this->get_rank($this->rank_min, $this->rank_max)];
>>  102     }
>>  103 }
>>  105 class Platoon_Officer extends Trooper {
>>  106     private $rank_min = 1;
>>  107     private $rank_max = 2;
>>  108     protected $rank_class = 'rank_officer';
>>  109
>>  110     public function __construct($params) {
>>  111         $this->age = $this->roll_age($this->rank_min , 4 + $this->rank_max);
>>  112         $this->gender = $this->roll_gender($params['percent_male']);
>>  113         $this->rank = $params[$this->rank_class][$this->get_rank($this->rank_min, $this->rank_max)];
>>  114     }
>>  115 }
>>  116
>>  117
>>  118 class Company_Officer extends Platoon_Officer {
>>  119     private $rank_min = 3;
>>  120     private $rank_max = 4;
>>  121
>>  122     public function __construct($params) {
>>  123         $this->age = $this->roll_age($this->rank_min , 7 + $this->rank_max);
>>  124         $this->gender = $this->roll_gender($params['percent_male']);
>>  125         $this->rank = $params[$this->rank_class][$this->get_rank($this->rank_min, $this->rank_max)];
>>  126     }
>>  127 }
>>  128
>>  129
>>  130 ?>
>> 
>> 
>> _______________________________________________
>> New York PHP User Group Community Talk Mailing List
>> http://lists.nyphp.org/mailman/listinfo/talk
>> 
>> http://www.nyphp.org/show-participation
> 
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> http://www.nyphp.org/show-participation
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20130125/e6264b90/attachment.html>


More information about the talk mailing list