-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn if property does not exist and add it to DiContainerTrait (#329)
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Core; | ||
|
||
/** | ||
* This trait implements https://github.com/php/php-src/pull/7390 for lower PHP versions | ||
* and also emit a warning when isset() is called on undefined variable. | ||
*/ | ||
trait WarnDynamicPropertyTrait | ||
{ | ||
protected function warnPropertyDoesNotExist(string $name): void | ||
{ | ||
'trigger_error'('Property ' . static::class . '::$' . $name . ' does not exist', \E_USER_DEPRECATED); | ||
} | ||
|
||
public function __isset(string $name): bool | ||
{ | ||
$this->warnPropertyDoesNotExist($name); | ||
|
||
return isset($this->{$name}); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function &__get(string $name) | ||
{ | ||
$this->warnPropertyDoesNotExist($name); | ||
|
||
return $this->{$name}; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function __set(string $name, $value): void | ||
{ | ||
$this->warnPropertyDoesNotExist($name); | ||
|
||
$this->{$name} = $value; | ||
} | ||
|
||
public function __unset(string $name): void | ||
{ | ||
$this->warnPropertyDoesNotExist($name); | ||
|
||
unset($this->{$name}); | ||
} | ||
} |