您好,感谢您的光临,
我想将一个变量($user
)从前一个函数传递给另一个函数,但我需要使用new函数的参数来传递将呈现这个新函数的值。
有没有办法可以将一个变量从另一个函数传递给一个只需要三个参数的新函数,而且它们都不是前一个函数的变量?
示例:
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
添加为另一个变量吗?
非常感谢您的帮助:)
答案 0 :(得分:3)
您可以将其作为参数传递,或者更好地执行此操作:
if ($user) my_function("italy", 19, "red");
因为您不必在该函数中使用$user
变量。
答案 1 :(得分:1)
您可以使用此功能,但最佳做法是使用课程。 即如果你调用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);
答案 2 :(得分:0)
嗯,全局变量是一种不好的做法,但在你的情况下是可行的。
我肯定会建议你研究/使用面向对象的方法。
有很多不同的方法可以实现你的尝试。
一种方法是将代码封装到对象中。
<?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 :(得分:0)
如果您之前的功能是这样的:
/**
* 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.