Jump to content

Archived

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

TheVillain9

PHP Classes

Recommended Posts

I'm a noob to PHP coding so please forgive my stupid questions.  I have two classes...

class Hotel {
var $name;
var $tariffs; // array of Tariff
var $availability;
var $location;
}
class Tariff {
var $fromDate;
var $toDate;
var $price;
var $currency;
}

Is it legit to place the Tariff class within the Hotel class? Secondly would this be the correct way to access a tariff?

$curhotel = // current hotel
$attrs['...'] = // just an XML value

$hotels[$curhotel]->tariffs[$attrs['FROMDATE']] = new Tariff();
$hotels[$curhotel]->tariffs[$attrs['FROMDATE']]->fromDate = $attrs['FROMDATE'];
$hotels[$curhotel]->tariffs[$attrs['FROMDATE']]->toDate = $attrs['TODATE'];

Lastly what is the difference in declaring a variable using "var" and not using it. Thanks so much for the quick help as  I just ordered a book so I do plan on learning it properly  :D

Share this post


Link to post
Share on other sites
Ehhh, I've never used var, my understanding was that "var" doesn't really exist in PHP.

I just use $variablename and that distinguishes that it's a variable, [the dollar sign that is].

Share this post


Link to post
Share on other sites
[quote author=SieRobin link=topic=108655.msg437336#msg437336 date=1158684227]
Ehhh, I've never used var, my understanding was that "var" doesn't really exist in PHP.

I just use $variablename and that distinguishes that it's a variable, [the dollar sign that is].
[/quote]
When coding OO (Object Oriented) code you need to add var before the variable you are defining. The code posted above is fine regarding the var keyword.

About putting a class inside another class PHP doesnt support this.

Share this post


Link to post
Share on other sites
you can't declare a class within another class, however, you can declare them both on the same page to assure they are both always present, and then, you can have a var in one class containing another class object:
[code]
<?php
class Hotel {
  var $name;
  var $tariffs; // array of Tariff
  var $availability;
  var $location;

  function Hotel() {
    // here is your constructor, you would need to use a class method or this method
    // to populate your tariff
    $this->tariffs = array();
  }

  function addTariff($x) {
    $this->tariffs[] = $x;
  }
}

class Tariff {
  var $fromDate;
  var $toDate;
  var $price;
  var $currency;

  function Tariff() {
    // here is your constructor
  }
}
?>
[/code]

basically, somewhere you have to determine where your tariffs are being set. whether or not that is in your constructor method, that is entirely up to you, but you can definitely have class objects that are instantiated within other objects. for instance, check this example out:
[code]
<?php
$hotel = new Hotel();
$t1 = new Tariff();
$t2 = new Tariff();
$hotel->addTariff($t1);
$hotel->addTariff($t2);
echo "<pre>\n";
var_dump($hotel->tariffs);
echo "</pre>\n";
?>
[/code]

hope this helps

Share this post


Link to post
Share on other sites

×

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.