PHP Classes
elePHPant
Icontem

PHP Decorator Pattern

Recommend this page to a friend!

      PHP Decorator Pattern  >  All threads  >  PHP Decorator Pattern  >  (Un) Subscribe thread alerts  
Subject:PHP Decorator Pattern
Summary:source code
Messages:2
Author:Chi Hoang
Date:2019-11-19 20:41:36
 

 


  1. PHP Decorator Pattern   Reply   Report abuse  
Chi Hoang - 2019-11-19 20:41:36
Hey mate,

nice package! what does it means:

1. function getFullName(): string <- : string?

2. function decorate(object $object, string $attributeName, $value, ?string $class = null): object <- ?string $class = null and also ):object?

Does it mean return object is string or object? Or only string or object is allowed?

3. function (...$arguments) use ($closure, $context) <- What means ...$arguments and use ...?

4. if (is_callable($this->__methods[$name] ?? null)) <- What means ?? null ?

Is it php7?

Also, it is possible to undecorate an object? Say I have decorate object with 2-3 decorates but I want to remove one of it. Is it possible?

Thanks!

  2. Re: PHP Decorator Pattern   Reply   Report abuse  
Aleksandar Zivanovic - 2019-12-11 14:54:51 - In reply to message 1 from Chi Hoang
Hello there,

First- Yes this is PHP 7.1+ compatible code.
Now to answer on other questions.

1. function getFullName(): string <- : string? -> It tells PHP runtime that it expect this method to return string.

2. function decorate(object $object, string $attributeName, $value, ?string $class = null): object <- ?string $class = null and also ):object? -> Typed arguments, it expect $class to be either NULL or string, and expect method to return an object (object is universal type)

3. function (...$arguments) use ($closure, $context) <- What means ...$arguments and use ...? -> ...$arguments will hold all provided parameters (arguments) in this one array.
keyword "use" is used to give access to certain variables inside function scope, in this case those variables are $closure and $context

4. if (is_callable($this->__methods[$name] ?? null)) <- What means ?? null ? ->
In case that "$this->_methods[$name]" doesn't exist it will provide NULL.
if it were e.g $this->_methods[$name] ?? 'Hello', it would provide string 'Hello', if $this->_methods[$name] doesn't exist

Also, it is possible to undecorate an object? Say I have decorate object with 2-3 decorates but I want to remove one of it. Is it possible? - Currently, it is not, but thanks for idea.