Instance declared in the variable that immediately afterwards receives a new value

2

I have the following code:

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

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

  • Is it okay to do this, can it cause some sort of problem?
  • Is there another more practical way to do something similar?
  • Is it possible to return values in instances of classes in addition to string with the __ toString ?
  

Issue

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% useless% variables because of this, I tried to apply the response example, but I could not!

    
asked by anonymous 16.08.2016 / 19:21

1 answer

2
  

Is it okay to do this, can it cause some sort of problem?

Well, I think that depends a lot on opinion, but if you want to hear mine, then there you go.

But at first, it has no problems. You are just overwriting a value of a variable.

For the sake of readability, I believe that each variable must "mean" something. In your case, $config = new Config in my opinion makes sense, since the name of the class is related to the name of the variable. However, when you declare $config = $config->getProperties() it loses some sense, because the final one, will you call the configuration properties?

So, in my opinion, it would make more sense:

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

Remember that this is my opinion, but using common sense is something that is good for everyone to apply. If you think that another person (if you are working together) will have to understand your code, then you will also have to worry about readability.

  

Is there another more practical way to do something similar?

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

For example, if you just want to access the Config::getProperties() method, without having to use a variable to store the instance of config (assuming you will not even use other methods, but only what is invoked below) , you could do so:

   use \Skreth\System\Config;

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

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

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

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

  

Can I return values in instances of classes in addition to string with __toString ?

Yes, and you can not imagine how many ways PHP has to solve this!

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

I'll give you some examples.

Example of the ArrayAccess implementation. You need to define 4 methods, which represent data definition, retrieval, deletion, and verification, 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 magic 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 __toString is related to different type returns (such as __toArray ), no, we do not have that yet in php.

    
17.08.2016 / 14:22