A
A
Alexey2015-12-04 19:11:17
PHP
Alexey, 2015-12-04 19:11:17

PHP links - good or bad?

I noticed that far-free often uses links
fatfreeframework.com/framework-variables
every time you need to add or change something
, it creates links, for example, simple code

$f3->set('key1.key2', 'val'); 
$f3->get('key1.key2');
$f3->set('key1', 'val1');
$f3->set('colors',array('red','blue','yellow'));
$f3->push('colors','green'); // works like PHP's array_push()
echo $f3->pop('colors'); // returns 'green'

this is convenient, but in all cases this happens (ref method)
$var=&$this->hive; // array([key1] => array([key2] = 
$var=&$var->$part;//part key1
$var=&$var->$part;//part key2
return $var;
//после массив выглядит как
 array(&[key1] => array(&[key2] =

full code of &ref($key,$add=TRUE) method
function &ref($key,$add=TRUE) {
    $null=NULL;
    $parts=$this->cut($key);
    //...
    if ($add)
      $var=&$this->hive;
    else
      $var=$this->hive;
    $obj=FALSE;
    foreach ($parts as $part)
      if ($part=='->')
        $obj=TRUE;
      elseif ($obj) {
        $obj=FALSE;
        if (!is_object($var))
          $var=new stdclass;
        if ($add || property_exists($var,$part))
          $var=&$var->$part;
        else {
          $var=&$null;
          break;
        }
      }
      else {
        if (!is_array($var))
          $var=array();
        if ($add || array_key_exists($part,$var))
          $var=&$var[$part];
        else {
          $var=&$null;
          break;
        }
      }
//...
    return $var;
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-12-04
@devalex89

more links to the god of links.
Links in and of themselves are neither good nor bad, the question is how to use them. For example, there are options in which we can replace recursion with a cycle with links. But in general, it is better not to use them and, if possible, make everything immutable. No state - no problems with tracking it, no various funny effects from the fact that you did not take into account that the value of the link has changed somewhere, etc. The code becomes easier to maintain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question