Jump to content

Question about Instantiating an obect


Go to solution Solved by trq,

Recommended Posts

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"

Link to comment
https://forums.phpfreaks.com/topic/275754-question-about-instantiating-an-obect/
Share on other sites

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

  • Solution

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.

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);
}
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.