Using PHP5 OO arrays for metatags generation on all pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DavidTheSlayer
Forum Newbie
Posts: 15
Joined: Fri Aug 22, 2008 2:17 am

Using PHP5 OO arrays for metatags generation on all pages

Post by DavidTheSlayer »

Hi, being a newbie at PHP5 OO I've searched on PHP's home site and couldn't find a solution, including on Google unfortunately.

Background...
I'm trying to get modular OO coding for my website via includes, so for example index.php will include the header, content, menu & footer etc. I'm trying to do this so that I can just add XHTML & other bits of PHP where needed. 8)

I want to have my own template class for webpages. The header page containing an include to the template class will be called, passing the tags to it.

The template class will take the metatags & give back the code to be outputted in the header.php. If there's a faster more scalable way for doing this then please tell me, as I always like to try & get my code as hi-performing as possible.

The prob

Here's my code so far for the template class called genMetaTags.php.

Code: Select all

<?php
class genMetaTags
{
    protected $metaTags = array();
        
        //Called when the object is initialised.
    function __construct($metaTags)
    {
        $this->tags = $metaTags;
                /*I'll another function to call so the tags & code can be generated
                again, if there's a better way please show me how*/
    }
    
        //Just in case for reusable code, so it can be overridden
    function changeTags($metaTags)
    {
        $this->tags = $metaTags;
    }
    
       //Do I really need this?
    function getTags()
    {
        return $this->tags;
    }
};
 
//Below is my code for header.php, note that I will put this into header.php once I get it working
 
$metaTags = new genMetaTags(array("1", "2", "3", "woo hoo"));
//$metaTags->tags(array("1", "2", "3", "woo hoo"));
 
//Show's array contents correctly
//var_dump($metaTags->tags);
 
$num = count($metaTags->tags);
//echo $num;
 
for ($i = 0; $i < $num; $i++)
{
    print $metaTags->getTags();
}
?>
Outputs without spaces...

Code: Select all

Array Array Array Array
Its driving me nuts...can someone help please.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Using PHP5 OO arrays for metatags generation on all pages

Post by mickeyunderscore »

You are having this problem because you aren't referencing an array index. You need to tell PHP which index you want to work with, e.g $arr[0] for the first index, or $arr[1] for the second.

Code: Select all

$m = $metaTags->getTags();
for ($i = 0; $i < $num; $i++)
{
     print $m[$i];
}
The above should fix your problem.

To answer the comment above getTags(): Generally speaking you should always set class variables as private/protected and use setters/getters to modify the contents if needed, so yes you do need the getter method.
DavidTheSlayer
Forum Newbie
Posts: 15
Joined: Fri Aug 22, 2008 2:17 am

Re: Using PHP5 OO arrays for metatags generation on all pages

Post by DavidTheSlayer »

Thank you very much, worked perfectly! :D

Completely forgot about indexes but didn't know how to access them using OO as I'm trying to move from procedural code to OO.

Got to love this forum! 8)
Post Reply