note 97081 added to language.oop5.inheritance
| From: | phpatsleepistheenemydotcodotuk at osu1 dot php dot net | Date: | Wed, 31 Mar 2010 15:47:36 +0000 |
| Subject: | note 97081 added to language.oop5.inheritance | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167562@lists.php.net to get a copy of this message | ||
Here's some example usage of the mixin class.
<?php
class Lunch extends Mix {
public $edible = true;
/*
* Circular references are, of course, illegal and will be detected
*/
/*
public function mixinSteakAndKidneyPie($method, $args){
return SteakAndKidneyPie::$method(@$args[0], @$args[1], @$args[2], @$args[3], @$args[4],
@$args[5]);
}
//*/
public function isEdible($affirm, $negate){
return $this->edible ? $affirm : $negate;
}
}
class Pie extends Mix {
/*
* class tokens are bound at compile time so need to be explicitly declared
* Need to make sure there are enough argument placeholders to cover all mixed in methods of
Lunch
* Late static binding may improve this situation
*/
public function mixinLunch($method, $args){
return Lunch::$method(@$args[0], @$args[1], @$args[2], @$args[3], @$args[4], @$args[5]);
}
public function buildPie($sep = ','){
return 'Crust' . $sep . $this->getFilling() . $sep . 'More Crust';
}
public function getFilling(){
$this->edible = false;
return 'Baking beans';
}
}
class SteakAndKidney extends Mix {
public function mixinLunch($method, $args){
return Lunch::$method(@$args[0], @$args[1], @$args[2], @$args[3], @$args[4], @$args[5]);
}
/*
* everything to be mixed in must be public
* protected/private methods called from within mixed in methods will fail
*/
public $filling = 'Steak and Kidney';
public function getFilling(){
$this->edible = true;
return $this->filling;
}
}
class SteakAndKidneyPie extends Mix {
/*
* order of mixin declaration significant
* later declarations override earlier ones
*/
public function mixinSteakAndKidney($method, $args){
return SteakAndKidney::$method(@$args[0], @$args[1], @$args[2], @$args[3], @$args[4],
@$args[5]);
}
public function mixinPie($method, $args){
return Pie::$method(@$args[0], @$args[1], @$args[2], @$args[3], @$args[4], @$args[5]);
}
/*
* Pick specific methods like so:
*/
//*
public function getFilling(){
return SteakAndKidney::getFilling();
}
//*/
}
$pie = new SteakAndKidneyPie();
echo $pie->buildPie(' | ');
echo '<br/>Pie ' . $pie->isEdible('is', 'is not') . '
Edible';
/*
OUTPUTS:
Crust | Steak and Kidney | More Crust
Pie is Edible
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 195.234.241.4
----
Manual Page -- http://www.php.net/manual/en/language.oop5.inheritance.php
Edit -- https://master.php.net/note/edit/97081
Del: integrated -- https://master.php.net/note/delete/97081/integrated
Del: useless -- https://master.php.net/note/delete/97081/useless
Del: bad code -- https://master.php.net/note/delete/97081/bad+code
Del: spam -- https://master.php.net/note/delete/97081/spam
Del: non-english -- https://master.php.net/note/delete/97081/non-english
Del: in docs -- https://master.php.net/note/delete/97081/in+docs
Del: other reasons-- https://master.php.net/note/delete/97081
Reject -- https://master.php.net/note/reject/97081
Search -- https://master.php.net/manage/user-notes.php