Current location: Hot Scripts Forums » Programming Languages » PHP » foreach and inherting classes


foreach and inherting classes

Reply
  #1 (permalink)  
Old 10-03-05, 09:22 AM
pinguïn pinguïn is offline
New Member
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question foreach and inherting classes

I' ve declared a class HtmlPage and a class HtmlSubPage that extends HtmlPage.
In HtmlPage I use an array and in HtmlSubPage I want to adjust it.

look at this sample of my code:

class HtmlPage{
//more declarations
public $styles;
public function __construct(){

//<!-- HtmlPage_constructor -->'
//more initialisations
$this->styles = array( array('href'=>'css/layout.css', 'media'=>'screen'));
}
}

class HtmlSubPage extends HtmlPage{
private $type;
private $path; //Path to main dir!

public function __construct($kind){
parent::__construct();
$this->type=$kind;
$this->path='../';
foreach($this->styles as $item)
$item['href']=$this->path.$item['href'];
//this has no effect in the html, but if I echo directly "$this->path.$item['href'];" then it outputs normal (../css/layout.css).
}
Has anyone got a clue how this is possible?

The complete source code is in the attached zip file!
Attached Files
File Type: zip source.zip (2.9 KB, 64 views)
Reply With Quote
  #2 (permalink)  
Old 10-03-05, 10:23 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
what exactly do you want to achive?
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 10-04-05, 01:43 AM
pinguïn pinguïn is offline
New Member
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I want to generate my html dynamically, the structure of my website is 1 root site and many subsites (like an foldersystem).
The class HtmlPage creates an root-page, the class HtmlSubPage creates an sub-page...
But there are some properties that stay the same for any page (CSS, footer-images, Javascript files to include) and its that path that is stored in the array.

But the weird thing was:

when I write:
PHP Code:

foreach($this->styles as $item)

   echo 
'<!-- '.$this->path.$item['href'].'-->'
I get the expected outcome,but when I write:
PHP Code:

foreach($this->styles as $item)

   
$item['href']=$this->path.$item['href']; 
Then none of the items in the array have been adjusted.

So the question is actually this:
Why isn't the value assigned to the array-item?

Last edited by pinguïn; 10-04-05 at 01:55 AM.
Reply With Quote
  #4 (permalink)  
Old 10-04-05, 03:10 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

This is because in this loop:
foreach($this->styles as $item)
$item['href']=$this->path.$item['href'];

the $item is a copy of the actual variable in the array. You must use key=>value pair in the loop:

foreach($this->styles as $k=>$item) {
$this->styles[$k]['href'] = $this->path . $this->styles[$k]['href'];
}

Also you could opt to use PHP5's
foreach($this->styles as &$item) {
$item['href']=$this->path.$item['href'];
}

but this breed of foreach had (or still has) errors: http://bugs.php.net/bug.php?id=31118
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 06:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.