Created
January 3, 2018 14:20
-
-
Save kobus1998/ebd8fe13cc30f629fe34966d1ee70076 to your computer and use it in GitHub Desktop.
Managing instance by using traits
This file contains 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 | |
/** | |
* Example class | |
*/ | |
class Best | |
{ | |
use Manager; | |
public $name; | |
function __construct ($name) | |
{ | |
$this->add($name); | |
$this->name = $name; | |
} | |
} |
This file contains 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 | |
/** | |
* Save instances staticly | |
* per class without having | |
* to extend or add code | |
* to all classes you want | |
* to save the instances of | |
*/ | |
trait Manager | |
{ | |
protected static $instances; | |
protected function add ($name) | |
{ | |
self::$instances[$name] = $this; | |
} | |
public function get ($name = null) | |
{ | |
if ($name != null) | |
{ | |
return self::$instances[$name]; | |
} | |
return self::$instances; | |
} | |
} |
This file contains 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 | |
/** | |
* Example class | |
*/ | |
class Test | |
{ | |
use Manager; | |
public $name; | |
function __construct ($name) | |
{ | |
$this->add($name); | |
$this->name = $name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment