将全局变量和参数传递给 PHP 中的函数

发布于 2024-11-29 08:09:29 字数 603 浏览 1 评论 0原文

您好,感谢您的光临,

我想将变量 ($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 之后的另一个变量?

非常感谢您的帮助:)

Hello and thanks for being there,

I would like to pass a variable ($user) from a previous function to another one, but I need to use the arguments of the new function to pass the values that will render this new one.

Is there any way I can pass a variable from another function to a new function that only expects three arguments, and none of them is the variable from the previous function?

Example:

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");

It works if I put inside function my_function:

global $user;

but I believe using global variables is not a good practice.

Any idea on how to pass it as an argument? Should I just add it as another variable after $colour in the arguments of the new function?

Thanks a lot for your help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虫児飞 2024-12-06 08:09:29

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

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

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

You can pass it as an argument, or better do this:

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

since you don't have to use the $user variable inside that function.

浪荡不羁 2024-12-06 08:09:29

您可以使用此函数,但最佳实践是使用类。
即如果你调用 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);

You can use this function but best practice will be using class.
i .e if you call my_function("italy", 19, "red"), $user will be false by default

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);
洒一地阳光 2024-12-06 08:09:29

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

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

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

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

<?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");

Well, global variables are a bad practice, but a viable one in your case.

I would definitely recommend you looking into/using a Object Oriented approached.

There are really a bunch of different ways to achieve what your trying.

One way would be to encapsulate your code into a object.

<?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");

Or you could implement a Singleton Pattern to easily get access to your object/functions from anywhere.

<?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");
别在捏我脸啦 2024-12-06 08:09:29

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

/**
 * 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.

If your previous function is something like this:

/**
 * 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.';
}

You can call it like this:

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文