steenkybastage Posted September 8, 2007 Share Posted September 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 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); Quote Link to comment Share on other sites More sharing options...
steenkybastage Posted September 8, 2007 Author Share Posted September 8, 2007 Ahhh... you had my hopes up. I still get the same thing without double quotes. Thanks tho. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 oops, try leaving out the dollar signs in your class variable declarations (also). $this->title = $title; $this->menu = $menu; $this->body = $body; $this->footer = $footer; Quote Link to comment Share on other sites More sharing options...
steenkybastage Posted September 8, 2007 Author Share Posted September 8, 2007 Ahhhhh... you're a GEEEEEEEENYUS (obviously I'm not!) Thank you, I think I owe you whatever the cost of a Rogaine session costs. I don't know why I put that there... Even when I started from scratch I messed it up. Thanks again for your help! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 glad i could help. i'd send you some hair, but i don't have enough of my own. please click Solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.