在函数之间传递变量 – php

以下是我实际代码的编辑版本:

<?PHP

include ('login_info.PHP');

class modernCMS {

var $host;
var $username;
var $password;
var $db;
var $url;


function connect(){
    $con = MysqL_connect($this->host, $this->username, $this->password);
    MysqL_select_db($this->db, $con) or die(MysqL_error());

MysqL_set_charset('utf8');

}


function get_coordinates(){

$sql ="select lat, lng from postcodes LIMIT 1;";
    $res = MysqL_query($sql) or die(MysqL_error());
    while($row = MysqL_fetch_assoc($res)){
        $lat = $row['lat'];
        $lng = $row['lng'];

    }
}


 function get_name(){

 $sql ="select name from places WHERE lat=$lat AND lng=$lng LIMIT 1;";
    $res = MysqL_query($sql) or die(MysqL_error());
    while($row = MysqL_fetch_assoc($res)){
        $name = $row['name'];

echo $name;


     }
}


?>

然后在一个单独的文档中,我有一个上面的文件包含.我使用以下函数调用函数get name:

<?=$obj->get_name()?>

get_name实际上包含一个用于计算两点之间距离的计算,但是因为它的冗长计算我将它留在上面的例子之外.

重要的是我可以使用$obj-> get_name()来获取$lat和$lng的输出

解决方法:

你正在遇到一个范围问题.变量仅适用于声明它们的函数.为了使它们可用,你可以显式地将变量传递给函数(你需要确保在display_coordinates()之前总是调用get_coordinates(),否则你将有未定义的值),或者使用全局变量(坏主意) .

最好的方法可能是为它制作一个类(虽然它取决于你打算如何使用它).您的变量将始终在范围内,并且在初始化变量之前,您不会冒险尝试运行display_coordinates()函数.

class Coordinate
{
    // These are the variables where the coords will be stored.
    // They are available to everything within the {}'s after 
    // "class Coordinate"  and can be accessed with
    // $this->_<varname>.
    protected $_lat;
    protected $_long;

    // This is a special function automatically called when 
    // you call "new Coordinate"
    public function __construct($lat, $long)
    {
        // Here, whatever was passed into "new Coordinate" is
        // Now stored in our variables above.
        $this->_lat  = $lat;
        $this->_long = $long;
    }

    // This takes the values are stored in our variables,
    // and simply displays them.
    public function display()
    {
        echo $this->_lat;
        echo $this->_long;
    }
}

// This creates a new Coordinate "object". 25 and 5 have been stored inside.
$coordinate = new Coordinate(25, 5); // 25 and 5 are Now stored in $coordinate.
$coordinate->display(); // Since $coordinate already "kNows" about 25 and 5
                        // it can display them.

// It's important to note, that each time you run "new Coordinate",
// you're creating an new "object" that isn't linked to the other objects.
$coord2 = new Coordinate(99, 1);
$coord2->display(); // This will print 99 and 1, not 25 and 5.

// $coordinate is still around though, and still kNows about 25 and 5.
$coordinate->display(); // Will still print 25 and 5.

您应该阅读Variable ScopeClasses and Objects以了解更多相关信息.

要将它与原始代码放在一起,你会做这样的事情,

function get_coordinates()
{
     return new Coordinate(25, 5);
}

function display_coordinates($coord)
{
    $coord->display();
}

$c = get_coordinates();
display_coordinates($c);
// or just "display_coordinates(get_coordinates());"

更新问题后编辑

您的代码中有一些不良做法,但这里有一些快速的步骤来获得您想要的内容.

// copy the Coordinate class from my answer above, but add two new
// lines before the final "}"
public function getLatitude()  { return $this->_lat; }
public function getLongitude() { return $this->_long; }

// Put the Coordinate class deFinition before this line
class modernCMS {

/////

// In your code, after this line near the top
var $url;

// Add this
var $coord;

/////

// In your get_coordinates(), change this...
$lat = $row['lat'];
$lng = $row['lng'];

// To this...
$this->coord = new Coordinate($lat, $lng);

/////

// In your get_name(), add two lines to the start of your function.
function get_name(){
    $lat = $this->coord->getLatitude();
    $lng = $this->coord->getLongitude();

与您的问题无关,但您还应该阅读“sql注入”,因为get_name()中的查询容易受到攻击.这里没什么大不了的,因为无论如何数据都来自你的其他查询,但仍然很好的做法是不在查询字符串中直接使用参数.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...