1

I am trying to convert some classes from Ruby to PHP for a project I am working on. I think I almost have it, but I am unversed in Ruby so I am struggling with understanding some aspects and what the equivalencies would be in PHP.

So the Ruby class is as follows:

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

Here is what I have written in PHP thus far: (I also added a note for what the original is supposed to be doing)

<?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;
        }
    }
}

I am trying to keep everything OOP to match the rest of my project.

I would really just like to know if I am doing this right or if I am way off. ;)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
W3bGuy
  • 735
  • 7
  • 20
  • `attr_reader` in ruby sets getters only for properties, so instead of writing them as `public` in php, make them private and write getter functions `getX()` – Shady Atef Feb 17 '17 at 18:13
  • @shady-atef - Thanks. I modified the OP to reflect those changes. Would something like that work? – W3bGuy Feb 17 '17 at 18:28
  • I am not sure about `next` is it special function or ruby or no .. So I will watch the question and wait for an answer – Shady Atef Feb 17 '17 at 18:33
  • I was just defining it as a function since it was in the rb class code. I was assuming from the notes that the next definition in the rb code was to find the next dot or coord set in the matching url set. I tried to see the best way to compare and found this: http://php.net/manual/en/function.next.php in the PHP docs. – W3bGuy Feb 17 '17 at 18:36

1 Answers1

0

I'm not good at ruby so I have a hard time understanding the code but this should be pretty accurate:

<?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;
    }
}

I'm not sure about the $points[$point['xy']] += 1; part as it doesn't really make sense to me so be sure to fix that as you see fit. Also, it is recommended to define setters manually instead of using the magic methods like __get().

Janis Vepris
  • 587
  • 3
  • 13