Ich versuche, einige Klassen von Ruby in PHP für ein Projekt zu konvertieren, an dem ich gerade arbeite. Ich denke, ich habe es fast, aber ich bin in Ruby nicht versiert, also habe ich Probleme mit dem Verständnis einiger Aspekte und was die Äquivalenzen in PHP wären.Ruby-Klasse in PHP umwandeln Klasse
So ist die Ruby-Klasse ist wie folgt:
class Log
def initialize (x,y,list,url)
@line = 0
@x=x
@y=y
@url=url
@list=list
@points = Hash.new(0)
@list.each do |point|
@points[point.xy] +=1
end
@reps = @points.values.max
end
attr_reader :x, :y, :list, :reps, :url
def next
coord = @list[@line]
@line += 1
return coord
end
end
Hier ist, was ich bisher in PHP geschrieben haben: (Ich habe auch eine Notiz für das, was das Original soll, zu tun)
<?php
/*
* Stores all the values pertinent to a single URL and gives accessors to them.
* There’s also a “next” method that returns next click within the same URL
*/
class Log
{
private $x;
private $y;
private $url;
private $list;
private $reps;
private $points;
function __construct($x,$y,$list,$url)
{
$this->line = 0;
$this->x = $x;
$this->y = $y;
$this->url = $url;
$this->list = $list;
$this->points = array();
foreach ($list as $l_attr => $l_val) {
if($l_attr == 'xy'){
$this->points[$l_val];
}
}
$this->reps = count($this->points);
return $this;
}
function next(){
$coord = next($this->list);
return $coord;
}
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
}
Ich versuche alles OOP auf den Rest meines Projekts zu halten.
Ich möchte wirklich nur wissen, ob ich das richtig mache oder ob ich weit weg bin. ;)
'attr_reader' in Ruby setzt Getter nur für Eigenschaften, also anstatt sie als' public' in PHP zu schreiben, mache sie privat und schreibe Getterfunktionen 'getX()' –
@ shady-atef - Danke. Ich habe das OP modifiziert, um diese Änderungen widerzuspiegeln. Würde so etwas funktionieren? – W3bGuy
Ich bin mir nicht sicher über 'nächste' ist es spezielle Funktion oder Rubin oder nicht .. So werde ich die Frage beobachten und auf eine Antwort warten –