June 2017
Intermediate to advanced
536 pages
9h 49m
English
The prototype pattern is about creating new objects by means of cloning them. This is quite a concept, as we are no longer using the new keyword to create new objects. The PHP language provides a special clone keyword to assist with object cloning.
The following example demonstrates a possible prototype pattern implementation:
<?phpclass Logger{ public $channel = 'N/A';}class SystemLogger extends Logger{ public function __construct() { $this->channel = 'STDIN'; } public function log($data) { return sprintf('Logging %s to %s.', $data, $this->channel); } public function __clone() { /* additional changes for (after)clone behavior? */ }}// Client use$systemLogger = new SystemLogger();echo $systemLogger->log('test');$logger ...
Read now
Unlock full access