Instance declared in variable that then receives a new value

Asked

Viewed 44 times

2

I have the following code:

$config = new \Skreth\System\Config(dirname(__DIR__) . '/config/config.ini');
$config = $config->getProperties();

I create a declared instance in the variable $config and soon after I give you a new value that is to receive an object. My doubts are:

  • It is correct to do this, can cause some kind of problem?
  • There is another more practical way to do something similar?
  • It is possible to return values in class instances other than string with the __toString?

Editing

How can I do the same with this?:

$SE_lang = $conn->prepare('SELECT Language FROM Language');
$SE_lang->execute();
$SA_lang = $SE_lang->fetchAll(\PDO::FETCH_ASSOC);

Is it possible to simplify it? Most of the time I create variables $SE_... useless because of this, I tried to apply the example of the answer, but I could not!

1 answer

2


It is correct to do this, can cause some kind of problem?

Well, I guess that depends a lot on your opinion, but if you want to hear mine, then here goes.

But at first, it’s fine. You’re just overwriting a variable value.

For the sake of readability, I believe that each variable must "mean" something. In your case, $config = new Config In my view it makes perfect sense, since the class name has to do with the variable name. However, when you declare $config = $config->getProperties() loses a little sense, because the end, you will call "configuration" the configuration properties?

So it would make more sense to me:

$config = new \Skreth\System\Config(dirname(__DIR__) . '/config/config.ini');
$properties = $config->getProperties();

It is worth remembering that this is my opinion, but using common sense is something that it is good for all people to apply. If you think someone else (if you are working together) will have to understand your code, then you will also have to worry about readability.

There is another more practical way to do something similar?

If this "more practical" means "simplify the sentence", yes, it exists. And of course, it will depend on the version of PHP you are using.

For example, if you just want to access the method Config::getProperties(), without having to use a variable to store the instance of config (taking into account that you won’t even use other method, but only what is invoked below), you could do so:

   use \Skreth\System\Config;

   $properties = (new Config((dirname(__DIR__) . '/config/config.ini'))->getProperties();

Thus, you simplify the call of getProperties, without having to store the instance in a variable to then call another method.

Note: Note that I used the use, to simplify the use of Config.

This functionality is called Class member access on instantiation and is available from PHP 5.4.

It is possible to return values in class instances other than string with the __toString?

Yes, and you can’t imagine how many ways PHP can solve this!

You can use the magic methods __get or __set, as well as being able to use the magic interface ArrayAccess, which allows you to provide an interface to your class, where you can access the data as if you were manipulating a array.

Let me give you some examples.

Example of the implementation of ArrayAccess. You need to define 4 methods, which represents the definition, obtaining, deletion and verification of the data, as if it were a array.

use Skreth\System\Config as BaseConfig;

class Config extends BaseConfig implements ArrayAccess
{
    public function offsetGet($key)     
    {
        return isset($this->props[$key]) ? $this->props[$key] : null;
    }

    public function offsetSet($key, $value)
    {
        $this->props[$key] = $value;
    }

    public function offsetExists($key)
    {
        return isset($this->props[$key]);
    }

    public function offsetUnset($key)
    {
        unset($this->props[$key]);
    }
}

$config = new Config('file.ini');

var_dump($config['db']); // chama offsetGet

var_dump(isset($config['db']); // chama offsetExists

$config['db'] = [/**...**/]; // chama offsetSet

unset($config['db']); // Chama offsetUnset

Example with magical methods __set and __get. I also used __unset and __isset, to look similar to the example above:

use Skreth\System\Config as BaseConfig;

class Config extends BaseConfig implements ArrayAccess
{

    public function __set($key, $value)
    {
        $this->props[$key] = $value;
    }

    public function __get($key)
    {
        return isset($this->props[$key]) ? $this->props[$key] : null;
    }

    public function __isset($key)
    {
        return isset($this->props[$key]);
    }

    public function __unset($key)
    {
        unset($this->props[$key]);
    }
}



$config = new Config('file.ini');

var_dump($config->db); // chama __get

var_dump(isset($config->db)); // chama __isset

$config->db = [/**...**/]; // chama __set

unset($config->db); // Chama __unset

Now, if the question about the __toString is related to different type returns (such as a __toArray), no, we don’t have it in php yet.

  • It’s good I add in the class Phpdoc for example @property array db?

  • @Viníciuslima is always good to document. Even because, depending on the implementation, you will want only one type of data to be accepted in the entry.

  • How can I do something similar with PDO class queries? It is possible, I added the question :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.