Jump to content

Question about Instantiating an obect


eldan88

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.