我想学习OOP但是我有一个问题如何将过程代码转换为OOP。如何将 function oblicz()中的过程代码转换为OOP标准?我是否必须为 $ wynik_skryptu 和 $ url 制作新课程?如何将它连接到一个函数oblicz()?现在,脚本需要很长时间来计算我的值。是否值得将其转换为OOP风格?
class Dystans {
public $obliczony;
public $szergeo1;
public $dluggeo1;
public $szergeo2;
public $dluggeo2;
function __construct($s1, $d1, $s2, $d2)
{
$this->szergeo1 = $s1;
$this->dluggeo1 = $d1;
$this->szergeo2 = $s2;
$this->dluggeo2 = $d2;
}
public function oblicz (){
$wynik_skryptu = array();
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins={$this->szergeo1},{$this->dluggeo1}&destinations={$this->szergeo2},{$this->dluggeo2}&mode=driving&language=en-EN&sensor=false&key=***************";
$data = @file_get_contents($url);
$wynik_skryptu = json_decode($data, true);
$this->obliczony = $wynik_skryptu['rows'][0]['elements'][0]['distance']['value'];
return ($this->obliczony)/1000;
}
}