2013-09-25 208 views
2

下面是我的實際代碼編輯版本:傳遞變量 - 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; 


    } 
} 


?> 

然後一個單獨的文件中我有一個包括用於上述的文件。我打電話使用功能得到名稱如下:

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

GET_NAME實際上包含了但是計算兩個點之間的距離計算,因爲它是一個耗時的計算我已經離開它的例子以上。

其重要的,我可以只使用obj- $> GET_NAME()來獲取輸出$ lat和$ LNG做的

+1

你可以將它們作爲參數 – Ibu

+1

如果你需要一個getter和setter,創建一個座標對象。 PHP是一種OOP語言:-) –

+0

您需要了解[variable scope](http://php.net/manual/en/language.variables.scope.php)。但是,使用諸如參數,返回值,數組,對象和屬性之類的東西都比使用全局變量更可取。 – Sammitch

回答

2

您正在運行到一個作用域的問題。變量僅適用於聲明它們的函數。爲了使它們可用,你可以將變量明確地傳遞給函數(你需要確保始終在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(); 

無關你的問題,但你也應該在get_name()閱讀「SQL注入」的查詢是脆弱的。這裏沒什麼大不了的,因爲數據來自您的其他查詢,但仍然不要直接在查詢字符串中使用參數。

+0

這個例子讓我感到困惑,因爲你似乎在兩個函數之外設置了值。在第一個函數中設置值的重要性在於它們來自sql語句。 –

+0

我會在代碼中添加一些註釋以嘗試使其更易於理解。 –

+0

我已添加評論,希望能夠更好地解釋發生的事情。雖然只有這麼一個簡短的答案,但只有這麼多。閱讀我最後提到的兩個文件將是最好的方式來充分理解正在發生的事情。不要被文檔長度拖延,這不是一開始就很容易理解的,但是範圍和類/對象都是一個重要的學習主題。 –

1

方式一:

function get_coordinates(&$lat, &$lng) 
{ 
    $lat = 25; 
    $lng = 5; 
} 

function display_coordinates($lat, $lng) 
{ 
    echo $lat; 
    echo $lng; 
} 

$lat = 0; 
$lng = 0; 

// assign values to variables 
get_coordinates($lat, $lng); 

// use function to display them... 
display_coordinates ($lat, $lng); 
3

功能的功能範圍內運行,因此您在get_coordinates()中設置的變量是局部變量。要創建全局變量,你可以使用全局關鍵字:

<?php 

function get_coordinates() 
{ 
global $lat, $lng; 
$lat = 25; 
$lng = 5; 
} 

function display_coordinates() 
{ 
global $lat, $lng; 
echo $lat; 
echo $lng; 
} 

get_coordinates(); 
display_coordinates(); 

或者$GLOBALS陣列:

<?php 

function get_coordinates() 
{ 
$GLOBALS['lat'] = 25; 
$GLOBALS['lng'] = 5; 
} 

function display_coordinates() 
{ 
echo $GLOBALS['lat']; 
echo $GLOBALS['lng']; 
} 

get_coordinates(); 
display_coordinates(); 

然而,這可能不是設置/最好的方式訪問這些變量,因爲任何功能可以改變他們的狀態在任何時候,你必須調用一個函數來設置它們,然後再調用另一個來顯示它們。如果你能描述你的具體目標,你可能會得到更好的建議。

一個更好做到這一點的方法是使用一類,並通過在你需要它的對象(這個簡單的例子不能證明正確encapsulation,但它是一個很好的起點):

<?php 

class Coordinates { 
    public $lat; 
    public $lng; 

    public function __construct($lat, $lng) { 
    $this->lat = $lat; 
    $this->lng = $lng; 
    } 

    public function display_coordinates() { 
    echo $this->lat . "\n"; 
    echo $this->lng . "\n"; 
    } 
} 

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

$coords = get_coordinates(); 
$coords->display_coordinates(); 


function output_coordinates($coordinates) { 
    $coordinates->display_coordinates(); 
} 
output_coordinates($coords); 

PHP中常用的另一種方法是在關聯數組(包含索引字符串的數組)中傳遞事物。我不通常喜歡這一點,因爲陣列並沒有聲明什麼打算持有,但它是一個選項:

<?php 

function get_coordinates() { 
    return array('lat' => 25, 'lng' => 5); 
} 

function output_coordinates($coordinates) { 
    echo $coordinates['lat'] . '\n'; 
    echo $coordinates['lng'] . '\n'; 
} 

$coords = get_coordinates(); 
output_coordinates($coords); 
+0

是類和對象自動*更好*? – 2013-09-25 23:24:13

+0

@ Dagon不?我也不是故意暗示這一點。但是它有助於依賴性,封裝和責任,因爲您將相似的屬性和方法組合在一起,消除了記住全局空間(以及在什麼點)的負擔。這段代碼不是更長或更復雜,而是更有組織,更容易擴展。 – Nicole

+0

其尖叫的大膽,讓我,但沒有包子的鬥爭意圖;-) – 2013-09-25 23:31:17

0

創建Coordinate.class.php文件:

<?php 
class Coordinate { 
    var $latitude; 
    var $longitude; 

    public function getLatitude() { 
    return $this->latitude; 
    } 

    protected function setLatitude($latitude) { 
    $this->latitude = floatval($latitude); 
    } 

    public function getLongitude() { 
    return $this->longitude; 
    } 

    protected function setLongitude($longitude) { 
    $this->longitude = floatval($longitude); 
    } 

    public function __construct() { 
    // Overload 
    if (func_num_args() == 2) { 
     $this->setLatitude(func_get_arg(0)); 
     $this->setLongitude(func_get_arg(1)); 
    } 
    // Default 
    else { 
     $this->setLatitude(0); 
     $this->setLongitude(0); 
    } 
    } 

    public function displayCoordinate() { 
    printf("Latitude: %.2f, Longitude: %.2f\n", 
     $this->getLatitude(), 
     $this->getLongitude()); 
    } 
} 

function main() { 
    $c = new Coordinate (25, 5); 
    $c->displayCoordinate(); 
} 

main(); 
?> 
0

另一個崗位的變化。我認爲更好的辦法:

function get_coordinates() 
{ 
    return array(
     "lat" => 25, 
     "lng" => 5 
    ); 

} 

function display_coordinates($latLongArray) 
{ 
    echo $latLongArray['lat']; 
    echo $latLongArray['lng']; 
} 


// assign values to variables 
$latLongArray = get_coordinates(); 

// use function to display them... 
display_coordinates ($latLongArray);