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
?– user45722
@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.
– Wallace Maxters
How can I do something similar with PDO class queries? It is possible, I added the question :D
– user45722