Skip to content

Commit

Permalink
Template: magic access via __get/__set replaced with object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 5, 2020
1 parent 9d8fe3a commit 961f708
Showing 1 changed file with 6 additions and 49 deletions.
55 changes: 6 additions & 49 deletions src/Bridges/ApplicationLatte/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@
*/
class Template extends LatteTemplate
{
/** @var array */
private $params = [];


/**
* Adds new template parameter.
* @return static
*/
public function add(string $name, $value)
{
if (array_key_exists($name, $this->params)) {
if (property_exists($this, $name)) {
throw new Nette\InvalidStateException("The variable '$name' already exists.");
}
$this->params[$name] = $value;
$this->$name = $value;

This comment has been minimized.

Copy link
@mabar

mabar Jan 6, 2020

Contributor

Template is for magic only, right? property_exists() check disallows define property, only magic properties can be set.

This comment has been minimized.

Copy link
@dg

dg Jan 6, 2020

Author Member

There are no magic properties at all. Only real properties.

This comment has been minimized.

Copy link
@mabar

mabar Jan 6, 2020

Contributor

This fails with The variable 'declared' already exists.

declare(strict_types=1);
class Template {
   public function add(string $name, $value){
        if (property_exists($this, $name)) {
            throw new Exception("The variable '$name' already exists.");
        }
        $this->$name = $value;
   }
}
class SpecificTemplate extends Template {
   public $declared;
}
$specific = new SpecificTemplate();
$specific->add('nondeclared', 'test');
$specific->add('declared', 'test');

This comment has been minimized.

Copy link
@dg

dg Jan 6, 2020

Author Member

It's an experiment. If it turns out that people actually inherit from a Template to create public or protected variables in a descendad, I'll find out why they do it and what to do with it.

return $this;
}

Expand All @@ -41,7 +37,9 @@ public function add(string $name, $value)
*/
public function setParameters(array $params)
{
$this->params = $params + $this->params;
foreach ($params as $k => $v) {
$this->$k = $v;
}
return $this;
}

Expand All @@ -51,47 +49,6 @@ public function setParameters(array $params)
*/
public function getParameters(): array
{
return $this->params;
}


/**
* Sets a template parameter. Do not call directly.
*/
public function __set(string $name, $value): void
{
$this->params[$name] = $value;
}


/**
* Returns a template parameter. Do not call directly.
* @return mixed value
*/
public function &__get(string $name)
{
if (!array_key_exists($name, $this->params)) {
trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
}

return $this->params[$name];
}


/**
* Determines whether parameter is defined. Do not call directly.
*/
public function __isset(string $name): bool
{
return isset($this->params[$name]);
}


/**
* Removes a template parameter. Do not call directly.
*/
public function __unset(string $name): void
{
unset($this->params[$name]);
return get_object_vars($this);

This comment has been minimized.

Copy link
@mabar

mabar Jan 6, 2020

Contributor

This will include not only public, but also protected variables

}
}

0 comments on commit 961f708

Please sign in to comment.