Skip to content

Instantly share code, notes, and snippets.

@fimak
Created March 5, 2015 10:33
Show Gist options
  • Save fimak/89d01b24f7f7a835153e to your computer and use it in GitHub Desktop.
Save fimak/89d01b24f7f7a835153e to your computer and use it in GitHub Desktop.
PHP Delegate pattern. Object instead of performing one of its stated tasks, delegates that task to an associated helper object. It helps us to inherit 2 methods from 2 different classes.
<?php
class A
{
public function aFunc()
{
echo __CLASS__;
}
}
class B
{
public function bFunc()
{
echo __CLASS__;
}
}
class C
{
private $_a;
private $_b;
public function __construct()
{
$this->_a = new A();
$this->_b = new B();
}
public function aFunc()
{
$this->_a->aFunc();
}
public function bFunc()
{
$this->_b->bFunc();
}
public function cFunc()
{
echo __CLASS__;
}
}
$cObj = new C();
$cObj->aFunc();
$cObj->bFunc();
$cObj->cFunc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment