user3664594
user3664594

Reputation: 191

PHP: Function in class returns nothing, but returns value outside of class

This is my script

<?php
require_once("simple_html_dom.php");

class adsl{

function up($lat,$lon){

$links2 = array();
$html = file_get_html('http://www.tpg.com.au/maps/getclosest.php?latitude='.$lat.'&longitude='.$lon.'&exch=GUESS');
foreach($html->find('tr') as $td2) {
 $links2[] = $td2->find('td',5)->plaintext;
}
$adsldown = $links2[3];
$adsldown = preg_replace('/[^0-9,]|,[0-9]*$/','',$adsldown); 
return $adsldown;
}
}
$test = new adsl();

$test->up('-37.8571449','144.8813738');

echo $adsldown;

$lat = '-37.8571449';
$lon = '144.8813738';
$links3 = array();
$html = file_get_html('http://www.tpg.com.au/maps/getclosest.php?latitude='.$lat.'&longitude='.$lon.'&exch=GUESS');
foreach($html->find('tr') as $td2) {
 $links3[] = $td2->find('td',5)->plaintext;
}
$adsldown2 = $links3[3];
$adsldown2 = preg_replace('/[^0-9,]|,[0-9]*$/','',$adsldown2); 
echo $adsldown2;

?>

When I try to query the function inside a class, it doesn't return anything eg. $adsldown is empty, but when I use the same code outside the class, it echos $adsldown2.

Upvotes: 1

Views: 68

Answers (1)

John Conde
John Conde

Reputation: 219794

You forgot to assign the results of your method call to $adsldown:

$adsldown = $test->up('-37.8571449','144.8813738');

echo $adsldown;

Upvotes: 1

Related Questions