Forum Moderators: coopster

Message Too Old, No Replies

object oriented confusion

trying to extend classes?

         

dnb_ovst

3:53 am on Oct 16, 2006 (gmt 0)

10+ Year Member



Hi guys, wondering if anyone can explain what I am doing wrong here... when I preview in my browser I get the following error:
Parse error: parse error, unexpected T_VARIABLE, expecting T_FUNCTION in \path\to\file.php on line 24*


<?php
class linkList
{
public $ar_linklist;
public function linkGenerator()
{
echo "<ul>";
foreach ($this->ar_linklist as $linkText => $linkRef)
{
echo "\t<li><a href=\"$linkRef\">$linkText</a></li>";
}
echo "</ul>";
}
}


class globalLinks extends linkList
{
[b]$ar_linklist['link a'] = "http://www.a.com"; *<- line 24[/b]
$ar_linklist['link b'] = "http://www.b.com";
$ar_linklist['link c'] = "http://www.c.com";
$ar_linklist['link d'] = "http://www.d.com";
$ar_linklist['link e'] = "http://www.e.com";
}


$globalLinks = new globalLinks;
$globalLinks->linkGenerator();
?>

Thanks in advance for any help!

scriptmasterdel

9:21 am on Oct 16, 2006 (gmt 0)

10+ Year Member



Below is how i would do it. You can not assign values to arrays in the core of the class it needs to be inside a function. So i added it to the function for you.


<?php

class linkList
{
var $ar_linklist;

function linkGenerator()
{
$this->ar_linklist = array('Link One'=>'http://www.link1.co.uk','Link Two'=>'http://www.link2.co.uk');

echo "<ul>";

foreach ($this->ar_linklist as $linkText => $linkRef)
{
echo "\t<li><a href=\"$linkRef\">$linkText</a></li>";
}

echo "</ul>";
}
}

class globalLinks extends linkList
{
function globalLinks()
{
$ar_linklist['link a'] = "http://www.a.com";
$ar_linklist['link b'] = "http://www.b.com";
$ar_linklist['link c'] = "http://www.c.com";
$ar_linklist['link d'] = "http://www.d.com";
$ar_linklist['link e'] = "http://www.e.com";
}
}

$globalLinks = new globalLinks;
$globalLinks->linkGenerator();

?>

I hope i have helped

Del

Psychopsia

2:56 pm on Oct 16, 2006 (gmt 0)

10+ Year Member



In the scriptmasterdel's code only the Link One and Link Two are shown.

My suggestions:
1] Define 'ar_linklist' as array in the parent class, like this:

class globalLinks extends linkList
{
var $ar_linklist = array();
...

2] Add $this-> inside the globalLinks constructor, else the vars assigned there will not available on the other class.

$this->ar_linklist['link a'] = "http://www.a.com";

3] If you want to add new links in the 'linkGenerator' function, append it to 'ar_linklist' var.

function linkGenerator()
{
$this->ar_linklist += array('Link One'=>'http://www.link1.co.uk','Link Two'=>'http://www.link2.co.uk');

dnb_ovst

5:16 pm on Oct 16, 2006 (gmt 0)

10+ Year Member



scriptmasterdel thanks for your reply & Psychopsia good looking out as always. I've gotten my logic to work by wrapping my $ar_linklist key/value pairs in the globalLinks object in a function as per scriptmasterdel's advice; also I used the $this keyword as per Psychopsia. I stayed up till 3am last night trying to figure that one out! unbelievable...

here is the final code:


class linkList
{
public $ar_linklist = array();
public function linkGenerator()
{
echo "<ul>\r\n";
foreach ($this->ar_linklist as $linkText => $linkRef){
echo "\t<li><a href=\"$linkRef\">$linkText</a></li>\r\n";
}
echo "</ul>\r\n";
}
}


class globalLinks extends linkList
{
public function globalLinks(){
$this->ar_linklist['link a'] = "http://www.a.com";
$this->ar_linklist['link b'] = "http://www.b.com";
$this->ar_linklist['link c'] = "http://www.c.com";
$this->ar_linklist['link d'] = "http://www.d.com";
$this->ar_linklist['link e'] = "http://www.e.com";
}
}


$globalLinks = new globalLinks;
$globalLinks->linkGenerator();