+
Skip to content

Add __isset() method to Config class to support for empty() checks on Config properties #105

@frugan-dev

Description

@frugan-dev

I've created a patch to support using empty($this->config->customKey):

diff --git a/src/Config.php b/src/Config.php
index 9fa94e6..3f90975 100644
--- a/src/Config.php
+++ b/src/Config.php
@@ -44,6 +44,17 @@ class Config implements ServiceInterface
         $this->config[$name] = $value;
     }
 
+    /**
+     * isset config
+     *
+     * @param  string $name
+     * @return boolean
+     */
+    public function __isset(string $name): bool
+    {
+        return isset($this->config[$name]);
+    }
+
     /**
      * check if has config
      *

As indicated in these answers:

https://stackoverflow.com/a/2045833/3929620
https://stackoverflow.com/a/25574839/3929620

without the __isset() method, $this->config->customKey resolves to null when checked with the empty() function.
For example, even if $this->config->customKey is not null, the following if statement does not pass:

if($this->config->has('customKey') && !empty($this->config->customKey)) {
    // This block is not executed
}

Do you think this is the right approach? Can I send you a PR?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载