Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lars-Janssen's avatar

Args in php?

Hi.

Does php have something like this (ruby example):

class Gear
 attr_reader :chainring, :cog, :wheel
 def initialize(args)
 @chainring = args[:chainring]
 @cog = args[:cog]
 @wheel = args[:wheel]
  end
  ...
  end
 
   Gear.new(
   :chainring => 52,
   :cog => 11,
   :wheel => Wheel.new(26, 1.5)).gear_inches 
0 likes
1 reply
Cronix's avatar
class Gear {
    public $chainring;
    public $cog;
    public $wheel;

    public function __construct($chainring, $cog, $wheel)
    {
        $this->chainring = $chainring;
        $this->cog = $cog;
        $this->wheel = $wheel;
    }
}
$wheel = new Wheel(26, 1.5);
$gear = new Gear(52, 11, $wheel);

echo $gear->chainring; //52
echo $gear->cog; // 11
echo $gear->wheel->gear_inches();  //execute gear_inches() method of $wheel, which is an instance of Wheel
2 likes

Please or to participate in this conversation.