Jump to content

[SOLVED] need help passing an array to class


steenkybastage

Recommended Posts

Hello,

 

I've been stuck for a couple of days on this... and I'm not eve sure it's possible. Have not had any success Googleing for answers, so here goes...

 

I'm working on learning OOP with php, and in the process I'm stuck trying to send an array to a class (which performs a function) to create a page with a menu system. I'm only in the beginning stages, as I can't seem to figure out why everything works UNTIL I send this to the class. Here's the general idea: (code to follow)

 

index.php consists of include files to add all functions and classes, then has a few variables that user can change: $title, $menu (array where I'm having problems), $body. Then I create a new variable off the class page as such: $b = new page("$title", "$menu", "$body");

 

The page class is working fine, until I try to give an array. I can echo out $title & $body fine, and I can echo out a non-array of $menu as well. When I echo out $menu I get "Array", when I echo out $menu[0] I get "A", $menu[1] is "r" etc (spells out Array).

 

The page class has a construct which sends each variable to the proper function, and everything else works. It is just obvious that the class isn't receiving an actual array, but the word "Array" instead. I can call the function from the index.php page (do_menu($menu)) and it will spit out the array, but once it goes to the class it is lost.

 

Sooo... all that 'splaining and I'm wondering if it's even possible to hand an array off to a class?!?

 

Here's the code:

index.php

<?php
echo 'index.php working<br />';
require_once('include/functions.php');
$title = 'A title!';
$menu = array('3','4','5','7');
$body = 'A body!';
$footer = 'A footer!';
$b = new page("$title", "$menu", "$body", "$footer");
?>

functions.php

<?php
echo 'functions.php working<br />';
require_once('html.php');
require_once('class/page.php');
?>

html.php

<?php
echo 'html.php working<br />';
function do_header($title)
{
?>
    <html>
    <head>
    <title><?php echo $title; ?></title>
    </head>
    <body>
    <?php
}
function do_menu($menu)
{
foreach ($menu as $value)
{
	echo "$value<br />";
}
}
function do_body($body)
{
echo "$body<br />";
}
function do_footer($footer)
{
echo $footer;
?>
    </body>
    </html>
    <?php
}
?>

page.php

<?php
echo 'class page working<br />';
class page
{
var $title;
var $menu;
var $body;
var $footer;
function __construct($title, $menu, $body, $footer)
	{
		$this->title = $title;
		$this->$menu = $menu;
		$this->$body = $body;
		$this->$footer = $footer;
		do_header($title);
		do_menu($menu);
		do_body($body);
		do_footer($footer);
	}
}
?>

 

Any help please? I'm not balding, but I'm about to look like it when I start pulling my hair out!

 

Thanks

Link to comment
Share on other sites

the problem is probably your use of double-quotes around the variable name when calling the function. instead of passing the actual array $menu, you are passing a string "$menu". there's probably no need for quotes around any of the passed variables. try this:

 

$b = new page($title, $menu, $body, $footer);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.