4

I am trying to create a custom class and have an array of those objects and I can't seem to get it to work. Here is my code:

class NavigationItem
{


private $_filename;
private $_linkname;

public function __construct($filename, $linkname)
{
    parent::__construct();

    $this->_filename = $filename;
    $this->_linkname = $linkname;
}

public function getFilename()
{
    return $_filename;  
}

public function getLinkname()
{
    return $_linkname;  
}
}

$navInfo = array();

$size = array_push($navInfo, new NavigationItem("index.php","HOME"),
                                  new NavigationItem("about.php","ABOUT"),
                                  new NavigationItem("coaches.php","STAFF"),
                                  new NavigationItem("summer.php","SUMMER 2011"),
                                  new NavigationItem("fall.php","FALL 2011"),
                                  new NavigationItem("history.php","HISTORY"),
                                  new NavigationItem("contact.php","CONTACT"));

echo "<table>";
echo "<tr>";
echo "<td colspan=\"7\" height=\"125px\"><img src=\"images/banner_blue_skinny.jpg\" alt=\"\" /></td>";
echo "</tr>";
echo "<tr>";

for($i=0; $i<$size; $i++)
{
echo "<td class=\"linkCell\"><a class=\"navigation\" href=\"" . $navInfo[$i]->getFilename() . "\">" . $navInfo[$i]->getLinkname() . "</a></td>";
}

echo "</tr>";

echo "<tr>";
echo "<td colspan=\"7\" class=\"gapCell\"></td>";
echo "</tr>";

echo ""; echo "";

Any ideas?

1
  • What parent class are you trying to parent::__construct();? You don't inherit from one? Commented Apr 28, 2011 at 15:05

2 Answers 2

7

$this is used to denote object of class inside the method of class and should be used to access the properties and to call methods inside other method of same class.

public function getFilename()
{
    return $this->_filename;  
}

Remove the call of parent construct from constructor of your class since it is not inheriting from any other class

//parent::__construct();
Sign up to request clarification or add additional context in comments.

2 Comments

Don't comment the parent::__construct(); as suggested in the answer, you should delete the line. Commenting code out is a bad habit.
@matt: you did not get me what I said Remove the call of... and I just showed him by commenting it
5
class NavigationItem
{

    private $_filename;
    private $_linkname;

    public function __construct($filename, $linkname)
    {
        $this->_filename = $filename;
        $this->_linkname = $linkname;
    }

    public function getFilename()
    {
        return $this->_filename;  
    }

    public function getLinkname()
    {
        return $this->_linkname;  
    }
}

To access a class property, you must use $this-> (not like in Java for example).

For better readeability, you can indent your code. parent::__construct() was useless too, because you don't inherit from another class.

Now for the array usage, just a couple of tricks that (IMO) make it more readable :

$navInfo = array(
    new NavigationItem("index.php","HOME"),
    new NavigationItem("about.php","ABOUT"),
    new NavigationItem("coaches.php","STAFF"),
    new NavigationItem("summer.php","SUMMER 2011"),
    new NavigationItem("fall.php","FALL 2011"),
    new NavigationItem("history.php","HISTORY"),
    new NavigationItem("contact.php","CONTACT")
);

foreach ($navInfo as $item) {
    echo $item->getFilename() . "..." . $item->getLinkname();
}

With foreach, you don't need to know the size of the array, you just iterate through each item (you don't care about the index of the items).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.