Err...until I remember to put parent::__construct() in B. :)
Leam On 01/25/2013 06:51 PM, Leam Hall wrote:
Interesting. The parent isn't multi-generational, far as I can see. That is, if C extends B which extends A, parent::__construct() in C does not have access to stuff constructed in A. I can make it work by making C extend A, but need to google a way to inherit further up the chain. Thanks! Leam On 01/25/2013 06:42 PM, Joey Derrico wrote:As Jeff said you have to call it explicitly class Trooper extends Person { public function _construct($parms) { parent::_construct(); //Whatever else is here } } Joey Derrico On Fri, Jan 25, 2013 at 5:57 PM, Rob Marscher <[email protected] <mailto:[email protected]>> wrote: 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 <[email protected] <mailto:[email protected]>> 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 <tel:970.443.9390> [email protected] <mailto:[email protected]> On Fri, Jan 25, 2013 at 5:47 PM, Leam Hall <[email protected] <mailto:[email protected]>> 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://lists.nyphp.org/mailman/listinfo/talk> http://www.nyphp.org/show-__participation <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_______________________________________________ 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
_______________________________________________ New York PHP User Group Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk http://www.nyphp.org/show-participation
