大家好,谢谢大家。
我想把一个变量($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
之后的新函数的参数?
非常感谢您的帮助
您可以将其作为参数传递,或者最好这样做:
if ($user) my_function("italy", 19, "red");
因为你不需要在函数中使用$user
变量
您可以使用这个函数,但最佳实践将使用class。例如,如果你调用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);
全局变量是一个不好的做法,但在你的情况下是可行的。
我绝对建议你使用面向对象的方法。
有很多不同的方法可以达到你想要达到的目标。
一种方法是将代码封装到对象中。<?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");
如果之前的函数是这样的:
/**
* 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.