【问题标题】:Passing global variables and arguments to a function in PHP将全局变量和参数传递给 PHP 中的函数
【发布时间】:2011-10-25 11:30:04
【问题描述】:

您好,感谢您的光临,

我想将一个变量 ($user) 从前一个函数传递给另一个函数,但我需要使用新函数的参数来传递将呈现这个新函数的值。

有什么方法可以将另一个函数的变量传递给一个只需要三个参数的新函数,并且它们都不是前一个函数的变量?

例子:

function my_function($country, $age, $colour) {
  if ($user = true) {
    echo "User is from " . $country . " and " . $age . " and his favourite colour is " . $colour; 
  }
}

my_function("italy", 19, "red");

如果我把函数放在 my_function 里面就可以了:

global $user;

但我认为使用全局变量不是一个好习惯。

关于如何将它作为参数传递的任何想法?我是否应该在新函数的参数中将其添加为 $colour 之后的另一个变量?

非常感谢您的帮助:)

【问题讨论】:

    标签: php variables arguments global


    【解决方案1】:

    如果你之前的函数是这样的:

    /**
     * Callback function for so_user_data() that tells if we want to give you info about the user or not.
     * @param (string) $user | Accepts a Username as input
     * @return (boolean) | true if the User is 'Rob'
     */
    function so_user_fn( $user )
    {
        $allowed_users = array(
             'Rob'
            ,'Jay'
            ,'Silent Bob'
        );
        if ( in_array $user, $allowed_users ) )
            return true;
        // false if not 'Rob'
        return false;
    }
    
    /**
     * Shows the country, age & fav. colour of a user or denies displaying this information
     * if the user is not a public v.i.p. or someone other we want to give you info about.
     * @uses so_user_fn() | used to determine if the user is 'Rob'
     * @param (string) $user | Username
     * @param (string) $country | (optional) Country of origin
     * @param (integer) $age | (optional) Age of the user
     * @param (string) $colour | (optional) Fav. Colour
     */
    function so_user_data( $user, $country = 'an unknown country', $age = 'unknown', $colour = 'not known' )
    {
        $output = "$user is from {$country} and {$age} years old. His favourite colour is {$colour}.";
    
        // Only print the output if the user is 'Rob'
        if ( so_user_test( $user ) )
            return print $output;
    
        return print 'We are not allowed to give you information about this user.';
    }
    

    你可以这样称呼它:

    so_user_data( 'Fancy Franzy' );
    // outputs: We are not allowed to give you information about this user.
    
    so_user_data( 'Rob' );
    // outputs: Rob is from an unknown country and unknown years old. His favourite colour is not known.
    
    so_user_data( 'Silent Bob', 'U.S.A.', '38', 'brown' );
    // outputs: Silent Bob is from U.S.A. and 38 years old. His favourite colour is brown.
    

    【讨论】:

      【解决方案2】:

      好吧,全局变量是一种不好的做法,但在您的情况下是可行的。

      我绝对建议您研究/使用面向对象的方法。

      确实有很多不同的方法可以实现您的尝试。

      一种方法是将您的代码封装到一个对象中。

      <?php
      
      class My_Cool_Class {
          public $user = false;
      
          public function myFunction($country, $age, $color) {
              if ($this->user)
              echo "User is from {$country} and {$age} years old and his favourite colour is {$color}"; 
          }
      }
      
      $class = new My_Cool_Class();
      $class->user = new User();
      $class->myFunction("Italy", 19, "red");
      

      或者你可以实现一个单例模式来轻松地从任何地方访问你的对象/函数。

      <?php
      
      class My_Cool_Class {
          public $user = false;
      
          protected static $_instance;
      
          public function getIntance() {
              if(!self::$_instance)
                  self::$_instance = new self();
      
              return self::$_instance;
          }
      
          public function setUser($user) {
              $this->user = $user;
          }
      
          public function myFunction($country, $age, $color) {
              if ($this->user)
                  echo "User is from {$country} and {$age} years old and his favourite colour is {$color}"; 
          }
      }
      
      
      //Set User from anywhere with this
      My_Cool_Class::getInstance()->setUser($user);
      
      
      //Call your function anywhere with this.
      My_Cool_Class::getInstance()->myFunction("Italy", 19, "red");
      

      【讨论】:

      • 哇,肖恩,你解释得很好,我要试一试,非常感谢!
      【解决方案3】:

      您可以使用此功能,但最佳做法是使用类。 即如果你调用 my_function("italy", 19, "red"), $user 默认为 false

      function my_function($country, $age, $colour, $user=false) {
      if ($user == true) {
      echo "User is from " $country . "and " . $age . " and his favourite colour is " . $colour; 
      }
      }
      
      my_function("italy", 19, "red",true);
      

      【讨论】:

        【解决方案4】:

        您可以将其作为参数传递,或者最好这样做:

        if ($user) my_function("italy", 19, "red");
        

        因为您不必在该函数中使用 $user 变量。

        【讨论】:

        • 感谢 Stewe,当你说我仍然可以将它作为参数传递时,我可以简单地将它添加到新函数的三个预期参数之后吗?
        • 当然,参数的顺序无关紧要,只要你在调用函数时使用相同的顺序
        • 唯一的缺点是变量不是真正的全局变量。它只是一个在它开始的范围内的变量。任何脚本、函数、方法都可以访问真正的全局变量。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-09
        • 1970-01-01
        相关资源
        最近更新 更多