OOP Array

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

User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

I´ve remade it abit...

OOP

Code: Select all

<?php
//Class named Index
class Index {
     protected $LinksNormal, $LinksSubOne;

     public function MenuUserNormal() {
         $this->linksNomal = array(
        	"<a href='Index.php?Action=Overview'>Overview</a>",
        	"<a href='Index.php'>Link Two</a>",
        	"<a href='Index.php'>Link Three</a>",
         	);
     	}
		
		public function MenuUserSubOne() {
         $this->LinksSubOne = array(
        	"<a href='Index.php?Action=Overview'>Overview</a>",
			"<a href='Index.php'>Link Sub</a>",
        	"<a href='Index.php'>Link Two</a>",
        	"<a href='Index.php'>Link Three</a>",
         	);
     	}

     public function GetLinksNormal() {
		
        return $this->LinksNormal;
     }
	
	public function GetLinksSubOne() {
		
        return $this->LinksSubOne;
     }
}
?>
Output page:

Code: Select all

 <?php 
 	require_once('Menu-OOP.php'); 
 	$Action = ($_GET['Action']);
	If ($Action == ""){
		$Action = "Home";
	}
        $links = New index();
		If ($Action == "Home"){
		echo "<br> Menu <br>";
                echo implode('<br> ', $links->GetLinksNormal() );
	        }
							
		ElseIf ($Action == "Overview"){
			echo "<br> Menu <br>";
			echo implode('<br> ', $links->GetLinksSubOne() );
		}
 ?>
I get Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\Banner\index.php on line 43


43:
echo implode('<br> ', $links->GetLinksNormal() );

I also get an error saying: Notice: Undefined index: Action in C:\wamp\www\Banner\index.php on line 5 Which means that the Get action is empty. (It is supposed to be empty the first time i get online. But how can i get "around the error"?

Hope this clarify (Or atleast help you understand better)
//Sorry for the confusion on the post before.
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: OOP Array

Post by drayfuss »

Okay, listen carefully. I did hint at this on your last post but it obviously got waved slightly.

You're code is peppered with miss-spellings and case sensitivity issues (your reference 'LinksNormal' in the class is actually 'LinksNomal'.

Ontop of that, you've forgotten to call the function MenuUserNormal() or MenuUserSubOne() before you request the array of links. So essantially you're asking the object for an empty variable, which hasn't been populated with your functions.

Rather than go through the code and try and flesh out the various spelling inconsistencies. Let me suggest this:

Code: Select all


<?php
//Class named Index
class Index {
	
	protected $linksNormal;
	protected $linksSubOne;
	
	public function __construct() {
		
		$this->linksNormal = array(
			"<a href='index.php?Action=Overview'>Overview</a>",
			"<a href='index.php'>Link Two</a>",
			"<a href='index.php'>Link Three</a>",
		);

		$this->linksSubOne = array(
			"<a href='index.php?Action=Home'>Home</a>",
			"<a href='index.php'>Link Sub</a>",
			"<a href='index.php'>Link Two</a>",
			"<a href='index.php'>Link Three</a>",
		);
	}
	
	public function getLinks($type) {
		
		$array = "links".$type;
		
		return $this->$array;
	
	}
}
?>

And index.php:

Code: Select all


<?php 

require_once('Menu-OOP.php'); 

$Action = ($_GET['Action']);

	if ($Action == "") {
    	$Action = "Home";
	}

	$links = new Index();

	if ($Action == "Home") {
	
    	echo "<br> Menu <br>";
    	echo implode('<br> ', $links->getLinks("Normal"));
	}

	elseif ($Action == "Overview") {
    
		echo "<br> Menu <br>";
    	echo implode('<br> ', $links->getLinks("SubOne"));
}

?>

You have to get out of the habit of using capital letters at the beginning of your words. If should be if. New should be new. While php is not always case sensitive, it's bad practice and will trip you up in the future.

A naming convention which has served programmers well is to start the names of methods and variables with a small letter: linksGetNormal instead of LinksGetNormal.

I've tried the above code on my development space. It works.

drayfuss
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

Ok thanks alot and i know about the case sensitivity "Just not all that into it just yet"

//Learning as we speak =)
//First attempt on OOP
//Thomas
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

Works like a charm

Thanks for all youre patiance and help =)

//Thomas
Post Reply