2017-02-17 3 views
1

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. ;)

+0

'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()' –

+0

@ shady-atef - Danke. Ich habe das OP modifiziert, um diese Änderungen widerzuspiegeln. Würde so etwas funktionieren? – W3bGuy

+0

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 –

Antwort

0

Ich bin bei Rubin nicht gut, so habe ich eine harte Zeit, den Code zu verstehen, aber dies sollte ziemlich genau sein:

<?php 

class Log { 
    private $line = 0; 
    private $x; 
    private $y; 
    private $url; 
    private $list; 
    private $reps; 

    public function __construct($x, $y, $url, $list) 
    { 
     $this->x = $x; 
     $this->y = $y; 
     $this->url = $url; 
     $this->list = $list; 
     $points = []; 

     foreach ($list as $point) { 
      $points[$point['xy']] += 1; 
     } 

     $this->reps = max($points); 
    } 

    public function getX() 
    { 
     return $this->x; 
    } 

    public function getY() 
    { 
     return $this->y; 
    } 

    public function getUrl() 
    { 
     return $this->url; 
    } 

    public function getList() 
    { 
     return $this->list; 
    } 

    public function getReps() 
    { 
     return $this->reps; 
    } 

    public function next() 
    { 
     $coord = $this->list[$this->line]; 
     $this->line += 1; 

     return $coord; 
    } 
} 

mir über den $points[$point['xy']] += 1; Teil bin nicht sicher, wie es nicht der Fall ist wirklich sinnvoll für mich, also achten Sie darauf, das zu beheben, wie Sie es für richtig halten. Außerdem wird empfohlen, die Setter manuell zu definieren, anstatt die magischen Methoden wie __get() zu verwenden.