Advertisement
Guest User

Untitled

a guest
Feb 14th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2.  
  3. // Try with Traits
  4.  
  5. trait HelpManagerTrait {
  6.  
  7.         protected $entries;
  8.  
  9.         public function __construct() {
  10.                 $this->entries = array();
  11.         }
  12.  
  13.         # DO NOT CALL
  14.        protected function _addEntry($entry) {
  15.                 $this->entries[] = $entry;
  16.         }
  17.  
  18.         public function printHelp() {
  19.                 print_r($this->entries);
  20.         }
  21.  
  22. }
  23.  
  24. class HelpManager {
  25.         use HelpManagerTrait;
  26.  
  27. }
  28.  
  29. class XYZ {
  30.  
  31.         protected $helpMan;
  32.  
  33.         public function __construct() {
  34.                 $this->helpMan = new HelpManager();
  35.         }
  36.  
  37.         public function getHelpMan() {
  38.                 return $this->helpMan;
  39.         }
  40.  
  41.         public function add($entry) {
  42.                 $test = $this->helpMan;
  43.                 $test = (HelpManagerTrait)$test;
  44.                 $test->_addEntry($entry);
  45.         }
  46.  
  47. }
  48.  
  49. $xyz = new XYZ();
  50. $xyz->add('test');
  51. $xyz->getHelpMan()->printHelp();
  52. $xyz->getHelpMan()->_addEntry('illegal');
  53. $xyz->getHelpMan()->printHelp();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement