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

OOP Array

Post by Goofan »

Hi!

I wonder how to define an array in OOP

I want a array caleld link to store links....

exampel:

Code: Select all

$Links(0) = "<a href='Index.php'>Link One</a>";
$Links(1) = "<a href='Index.php'>Link Two</a>";
$Links(2) = "<a href='Index.php'>Link Three</a>";
and then catch them in another file and output all of them...

Detailed explination
I use OOP and as i call the OOP function i should get an array with links in it
then i should output those links so that the user can see them...

Any help appriciated =)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP Array

Post by Christopher »

Code: Select all

class Foo {
     protected $links;

     public function __construct() {
          $this->links = array(
                    "<a href='Index.php'>Link One</a>", 
                    "<a href='Index.php'>Link Two</a>", 
                    "<a href='Index.php'>Link Three</a>",
                    );
     }

     public function getLinks() {
          return $this->links;
     }

}
(#10850)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

Thank you christofer! =)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

How do you collect an array at Output part?

I tried

Code: Select all

$links = New foo();
echo $links->GetLinks();
I Get:
[text]Array[/text]
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: OOP Array

Post by drayfuss »

For a breakdown of an associative array, you can use the simple php function print_r().

Code: Select all


$links = new Foo();
print_r($links->getLinks());

Or you could store them in an array and pluck out the ones you want:

Code: Select all


$links = new Foo();
$links_array = $links->getLinks();

echo('second links: '.$links_array[1]);

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP Array

Post by Christopher »

Goofan wrote:How do you collect an array at Output part?

Code: Select all

$links = New foo();
echo implode(', ', $links->GetLinks() );
(#10850)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

Thanks alot =)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP Array

Post by Christopher »

It might be better to put the implode() inside the getLinks() method. If you add links individually then keeping them in an array makes sense. But if you always want a string containing the links, then put that functionality inside getLinks().
(#10850)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

Ok so I made it sofar however if I change the array and input it into another function not the constructer it fails...
I Got this:
OOP

Code: Select all

<?php
//Class named Index
class Index {
     protected $links;

     public function MenuUserNormal() {
         $this->links = 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 MenuUserSub() {
         $this->links = array(
        	"<a href='Index.php?Action=Overview'>Overview</a>",
                "<a href='Index.php'>Overview Sub</a>",
        	"<a href='Index.php'>Link Two</a>",
        	"<a href='Index.php'>Link Three</a>",
         	);
     	}

     public function GetLinks() {
		
        return $this->links;
     }
	
	public function GetLinksNormal() {
		
        return $this->links;
     }
}
?>
Exampel outputter:

Code: Select all

 <?php require_once('Menu-OOP.php'); 
 
 $Action = ($_GET['Action']);

 $link = New index();
 
If ($Action == "Home"){
  $links = $link->MenuUserNormal();
  echo "<br> Menu <br>";
  echo implode('<br> ', $links->GetLinksNormal() );
 }
							
 ElseIf ($Action == "Overview"){
  $links = $link->MenuUserNormal();
  echo "<br> Menu <br>";
  echo implode('<br> ', $links->GetLinks() );
 }
 ?>
If you understand the problem then please tell me what to do to make it go away.
"Im sorry if I don´t make any sense" Im extreamly tired and over worked at the moment...

//Thanks in advance
//Thomas
Last edited by Goofan on Sat May 15, 2010 6:29 pm, edited 1 time in total.
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: OOP Array

Post by drayfuss »

I think you're mistaking the $link variable with the $links variable. If you want to call the function inside you Index instance, then new need to refer to it with the right variable.

Try this:

Code: Select all


 <?php require_once('Menu-OOP.php');
 
$Action = ($_GET['Action']);

$link = New index();
 
if ($Action == "Home"){
$link->MenuUserNormal();
echo "<br> Menu <br>";
echo implode('<br> ', $link->GetLinksNormal() );
}
                                                       
else if ($Action == "Overview"){
$link->MenuUserNormal();
echo "<br> Menu <br>";
echo implode('<br> ', $link->GetLinks() );
}
?>

You were calling $links->GetLinks() where $links is not an instance of Index. $link is, however.

As a side note, ensure you're using the right case. Functions and variables are usually case sensitive in most languages.

drayfuss
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP Array

Post by Christopher »

You need to keep the constructor. It is the method that is run when the object is created.

Code: Select all

<?php
class Menu {
     protected $links;

     public function __construct() {
         $this->links = 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 GetLinks() {
        return $this->links;
     }
	
     public function GetLinksString($separator) {
        return implode($separator, $this->links);
     }
}
Example outputter:

Code: Select all

<?php require_once('Menu-OOP.php'); 
 
$Action = ($_GET['Action']);

$menu = new Menu();
 
if ($Action == "Home") {
  echo "<br> Menu <br>";
  echo $menu->GetLinksString('<br/>');
} elseif ($Action == "Overview"){
  echo "<br> Menu <br>";
  echo implode('<br/> ', $menu->GetLinks() );
}
(#10850)
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: OOP Array

Post by drayfuss »

Lack of a class constructor isn't the issue.

A __construct() function isn't required in class, though it's good practice. So long as you call $link->MenuUserNormal() you should still get the desired effect.

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

Re: OOP Array

Post by Goofan »

Sorry I made a misstake "Some of the code where left out" Ill try the suggestions tomorrow =) Thanks alot

(EDITED the old code
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP Array

Post by Goofan »

Someone got an ide? I dont think youre code works like it should considering the changes i made to the original code... "EDITED CODE post" Making the code complete...

Relook at it please. =)

//Thanks in advance
//Thomas
Last edited by Goofan on Mon May 17, 2010 2:14 pm, edited 1 time in total.
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: OOP Array

Post by drayfuss »

Could you remind me of what the actual problem is, and which post you've uploaded your 'definitive' code.

That way I might be able to help.

Thanks,
drayfuss
Post Reply