Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Array of Custom Class

Tags:

arrays

php

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?

like image 226
rplankenhorn Avatar asked Dec 16 '22 14:12

rplankenhorn


2 Answers

$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();
like image 80
Shakti Singh Avatar answered Jan 13 '23 17:01

Shakti Singh


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).

like image 40
Matthieu Napoli Avatar answered Jan 13 '23 16:01

Matthieu Napoli