TheVillain9 Posted September 19, 2006 Share Posted September 19, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21296-php-classes/ Share on other sites More sharing options...
SieRobin Posted September 19, 2006 Share Posted September 19, 2006 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 Link to comment https://forums.phpfreaks.com/topic/21296-php-classes/#findComment-94744 Share on other sites More sharing options...
wildteen88 Posted September 19, 2006 Share Posted September 19, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/21296-php-classes/#findComment-94748 Share on other sites More sharing options...
obsidian Posted September 19, 2006 Share Posted September 19, 2006 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]<?phpclass 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 Quote Link to comment https://forums.phpfreaks.com/topic/21296-php-classes/#findComment-94762 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.