Page 1 of 2
OOP Array
Posted: Thu May 13, 2010 6:15 pm
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 =)
Re: OOP Array
Posted: Thu May 13, 2010 8:06 pm
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;
}
}
Re: OOP Array
Posted: Fri May 14, 2010 7:23 am
by Goofan
Thank you christofer! =)
Re: OOP Array
Posted: Fri May 14, 2010 7:40 am
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]
Re: OOP Array
Posted: Fri May 14, 2010 11:42 am
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]);
Re: OOP Array
Posted: Fri May 14, 2010 1:02 pm
by Christopher
Goofan wrote:How do you collect an array at Output part?
Code: Select all
$links = New foo();
echo implode(', ', $links->GetLinks() );
Re: OOP Array
Posted: Fri May 14, 2010 6:09 pm
by Goofan
Thanks alot =)
Re: OOP Array
Posted: Fri May 14, 2010 6:28 pm
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().
Re: OOP Array
Posted: Sat May 15, 2010 9:06 am
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
Re: OOP Array
Posted: Sat May 15, 2010 9:36 am
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
Re: OOP Array
Posted: Sat May 15, 2010 12:49 pm
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() );
}
Re: OOP Array
Posted: Sat May 15, 2010 1:01 pm
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
Re: OOP Array
Posted: Sat May 15, 2010 6:27 pm
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
Re: OOP Array
Posted: Mon May 17, 2010 8:45 am
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
Re: OOP Array
Posted: Mon May 17, 2010 10:29 am
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