Created
November 11, 2015 02:23
-
-
Save philbirnie/f460b0e098df7bf2e024 to your computer and use it in GitHub Desktop.
PHP Book insteadof and as.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* If two traits share the same method and are both included in a class, we have a problem. Rare, but possible | |
* Here's how to get around it. | |
* | |
* Reference: PHP Objects, Patterns and Practice, ed 4. pp 49 | |
**/ | |
trait TaxTools { | |
function calculateTax($price) { | |
return 123; | |
} | |
//More stuff. | |
} | |
trait PriceUtilities { | |
private $taxRate = 0.075; | |
function calculateTax($price) { | |
return $price * (1 + $taxRate); | |
} | |
//More stuff. | |
} | |
// | |
abstract class Service { | |
} | |
class UtilityService extends Service { | |
/** Use Tax Tools' version of calculate tax rather than Price Utilities' **/ | |
use PriceUtilities, TaxTools { | |
TaxTools::calculateTax instead of PriceUtilities; | |
} | |
} | |
/** | |
* OR .... | |
*/ | |
class UtilityService extends Service { | |
/** Use Tax Tools' version of calculate tax rather than Price Utilities' but | |
* cast PriceUtilities' calculateTax method as basicTax so that it can still be used | |
**/ | |
use PriceUtilities, TaxTools { | |
TaxTools::calculateTax instead of PriceUtilities; | |
PriceUtilities::calculateTax as basicTax; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment