php 变量的实际用途是什么?

vx6bjr1n  于 2023-03-21  发布在  PHP
关注(0)|答案(7)|浏览(233)

可变变量看起来很酷,但是我想不出在生产环境中实际使用它们的场景。这样的场景会是什么?它们是如何使用的?

7uzetpgm

7uzetpgm1#

我猜,它的目的是允许新手程序员在不使用复合类型(数组和对象)等“复杂的东西”的情况下动态地更改数据。
我从来没用过。

ctehm74n

ctehm74n2#

variable变量本质上是一个数组(map/dictionary)。下面是等价的概念:

<?php
$foo = array('a' => 1);
$bar = 'a';
echo $foo[$bar]."\n";

$foo_a = 1;
$bar = 'a';
$vv = "foo_$bar";
echo $$vv."\n";
?>

因此,如果你把“可变变量” Package 到父数组中,你就可以去掉它们。
我见过人们在类中使用变量属性:

<?php
class Foo
{
  private $a = 1;

  public function __get($key)
  {
    if (isset($this->$key)) return $this->$key;
  }
}

$foo = new Foo();
echo $foo->a;
?>

但是,你也可以使用数组:

<?php
class Foo
{
  private $props = array('a' => 1);

  public function __get($key)
  {
    if (array_key_exists($key, $this->props))
      return $this->props[$key];
  }
}

$foo = new Foo();
echo $foo->a;
?>

课外:

<?php
class Foo
{
  public $a = 1;
}

$foo = new Foo();
$prop = 'a';
echo $foo->{$prop};
?>

所以在编写自己的受控代码时,你永远不必“必须”使用变量或变量属性。我个人的偏好是永远不要使用变量变量。我偶尔会使用变量属性,但当我以这种方式访问数据时,我更喜欢使用数组。

py49o6xq

py49o6xq3#

我发现它在一个场景中很有用。

$obj->media$title => Video title

所以我用它就像

$mt = 'media$title';
$obj->$mt ;

所以它在这里对我起作用了:)

qacovj5a

qacovj5a4#

就我个人而言,我经常使用它们。以下类型的所有调用都使用variable-variables:

$foo->$bar = 'test';
$foo->$bar();
$bar();

所以任何时候你做一个动态的方法/函数调用,你都在使用变量-变量……
这种方法的一个常见用法是通过__get魔术方法访问受保护的属性。我经常看到以下情况:

public function __get($name) {
    return isset($this->$name) ? $this->$name : null;
}

根据定义,它使用可变变量来提供对受保护成员的读访问。
我从来没有直接使用过$$var语法(我想我永远也不会)。我见过它被用来访问名为global $$name; echo $$name;的全局变量,但同样的事情也可以用$_GLOBALS[$name]语法来完成,所以这不是一个好的用例(更不用说使用全局变量通常被视为不好的实践)...

ltskdhd1

ltskdhd15#

考虑一下在模板系统中使用它,您正在使用PHP文件并需要设置变量:

function fetch_template($file, $vars){
    $ret = 'File not loaded.';
    if(file_exists(TEMPLATE_PATH.$file)){
        //could do this with extract() but I am showing you
        foreach($vars as $varName => $value){
            ${$varName} = $value;
        }
        ob_start();
        include(TEMPLATE_PATH.$file);
        $ret = ob_get_contents();
        ob_end_clean();
    }
    return $ret;
}

现在假设您在模板中使用了这些变量名,您可以调用它并将变量传递给它以供使用。

echo fetch_template('hi_there.tpl', array('name'=>'JJ'));

然后在模板中:

Hello <?php echo $name; ?>!
fcipmucu

fcipmucu6#

我主要用它来减少在php文件开头清理get/post数据时的复制粘贴:它使用正确的名称创建经过清理的变量:

$fields=array('age','name','gender','email','username');

foreach($fields as $field) {
    if (empty($_REQUEST[$field] === false)
        ${$field} = sanitize($_REQUEST[$field]);
    else
        ${$field} = '';
}

而不是所有这些行:

if (empty($_GET['age']) === false) 
    $age= sanitize($_GET['age']);
else
    $age= '';

if (empty($_GET['name']) === false) 
    $name= sanitize($_GET['name']);
else
    $name = '';

if (empty($_GET['gender']) === false) 
    $gender= sanitize($_GET['gender']);
else
    $gender= '';

if (empty($_GET['email']) === false) 
    $email= sanitize($_GET['email']);
else
    $email= '';

if (empty($_GET['username']) === false) 
    $username= sanitize($_GET['username']);
else
    $username= '';

希望能有所帮助

mspsb9vt

mspsb9vt7#

我知道这是一个老的线程,变量不太被使用。事实上,如果它在未来的版本中被弃用,我也不会感到惊讶。
但我发现它作为switch语句的替代非常有用,它更简洁,简单和简短的解决方案。而且它似乎比switch语句执行得更有效。
所以这个:

class AlterSwitch{

    public static function the_case($option){

        $dirname = dirmane(__DIR__);

        switch ($option){
            case 'root':
                $section = $dirname . '/templates/root.html';
                break;
            case 'head':
                $section = $dirname . '/templates/head.html';
                break;        
            case 'item_cont':
                $section = $dirname . '/templates/item_cont.html';
                break;
            case 'item':
                $section = $dirname . '/templates/item.html';
                break;
    }
    return $section;

}

可以变成这样:

class AlterSwitch{

    public static function the_case($option){

        $dirname = dirmane(__DIR__);

        $root = $dirname . '/templates/root.html';
        $head = $dirname . '/templates/head.html';
        $item_cont = $dirname . '/templates/item_cont.html';
        $item = $dirname . '/templates/item.html';

        if($$option!==null){
            # Variable variable is the parameter and returns the Variable of this class
            return $$option;
    } 
    return "File not found";  
    }

}

// Instantiate e.g.
// For both classes
// returns $section - $head

AlterSwitch::the_case('head'));

相关问题