eldan88 Posted March 17, 2013 Share Posted March 17, 2013 Hey, I am new to OOP and just started using it. I wrote a simple question that calls a function that does an ehco. But for some reason everytime I call it it echo's the string twice. I'm not sure why. Below is the code i wrote. <?php class Train { var $train="e train"; function train() { echo "I am in the " .$this->train; } } $train = new Train; $train->train(); ?> It echos out the following "I am in the e trainI am in the e train" Quote Link to comment https://forums.phpfreaks.com/topic/275754-question-about-instantiating-an-obect/ Share on other sites More sharing options...
requinix Posted March 17, 2013 Share Posted March 17, 2013 It's a holdover from the days of PHP 4: if you have a method with the same name as the class then it will consider that to be the constructor. Rename the method or define a __construct() method (it can be empty, just matters that it exists). Quote Link to comment https://forums.phpfreaks.com/topic/275754-question-about-instantiating-an-obect/#findComment-1419079 Share on other sites More sharing options...
Solution trq Posted March 17, 2013 Solution Share Posted March 17, 2013 You could also put the class in a namespace which removes this backward compatibility. <?php namespace Railway; class Train { public $train = "e train"; public function train() { echo "I am in the " .$this->train; } } $train = new Train; $train->train();On a side note, use "public" in place of the older "var" keyword and you should also be explicit about your methods visibility IMO. Quote Link to comment https://forums.phpfreaks.com/topic/275754-question-about-instantiating-an-obect/#findComment-1419081 Share on other sites More sharing options...
ignace Posted March 17, 2013 Share Posted March 17, 2013 Here's an expanded example of your train in OO, for brevity most functions do not include code: namespace Railway; /* used as a flyweight */ class Passenger { } class PassengerFactory { const ECONOMY = 2; const BUSINESS = 1; private $economyPassenger; private $businessPassenger; public function __construct() { $this->economyPassenger = new Passenger; $this->businessPassenger = new Passenger; } public function makePassenger($class = self::ECONOMY) { if ($class === self::BUSINESS) { return $this->getBusinessPassenger(); } else { return $this->getEconomyPassenger(); } } public function getEconomyPassenger() { return $this->enconomyPassenger; } public function getBusinessPassenger() { return $this->businessPassenger; } } class Station {} class StationRoute {} class Schedule { private $stationRoute; private $timeTable; } class Train { private $id; private $locomotive; private $wagons = array(); private $stationRoute; private $schedule; public function __construct($id, Locomotive $locomotive, Array $wagons, StationRoute $route, Schedule $schedule) {} public function board(Passenger $passenger) {} public function attach(Wagon $wagon) {} } /** @internal */ class SplFixedArray { public function isAtCapacity() { return $this->count() === $this->getRealSize(); } public function getRealSize() { return count(array_filter($this->toArray())); } } /** @internal */ class Locomotive extends SplFixedArray { public function __construct($maxWagons) { parent::__construct($maxWagons); } } /** @internal */ class Wagon extends SplFixedArray { public function __construct($capacity) { parent::__construct($capacity); } } Quote Link to comment https://forums.phpfreaks.com/topic/275754-question-about-instantiating-an-obect/#findComment-1419094 Share on other sites More sharing options...
eldan88 Posted March 18, 2013 Author Share Posted March 18, 2013 Oh okay i see. Thank you guys it makes a lot of sense. Quote Link to comment https://forums.phpfreaks.com/topic/275754-question-about-instantiating-an-obect/#findComment-1419343 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.