Array Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • O.B.

    Array Problems

    In the code below, the aboutMenuNames array gets overwritten with the
    values in mainMenuNames in the constructor. What am I doing wrong?


    class dynamicMenu {

    var $mainMenuNames;

    var $aboutMenuNames ;

    //
    // Constructor
    //
    function dynamicMenu() {
    $this->$aboutMenuName s = array("About Us",
    "Statement of Faith",
    "About Our Pastor",
    "Directions to Us",
    "Contact Us");

    $this->$mainMenuNam es = array("Home",
    "AboutUs",
    "ForAdults" ,
    "ForTheKids ",
    "ForTheYout h",
    "WantToServ e");


    }
    };

  • Andy Hassall

    #2
    Re: Array Problems

    On Sat, 13 Nov 2004 12:07:51 -0600, "O.B." <funkjunk@bells outh.net> wrote:
    [color=blue]
    >In the code below, the aboutMenuNames array gets overwritten with the
    >values in mainMenuNames in the constructor. What am I doing wrong?
    >
    >
    >class dynamicMenu {
    >
    > var $mainMenuNames;
    >
    > var $aboutMenuNames ;
    >
    > //
    > // Constructor
    > //
    > function dynamicMenu() {
    > $this->$aboutMenuName s = array("About Us",
    > "Statement of Faith",
    > "About Our Pastor",
    > "Directions to Us",
    > "Contact Us");
    >
    > $this->$mainMenuNam es = array("Home",
    > "AboutUs",
    > "ForAdults" ,
    > "ForTheKids ",
    > "ForTheYout h",
    > "WantToServ e");
    >
    >
    > }
    >};[/color]

    You've accidentally used variable variables.

    $a = 'b'; // sets $a
    $$a = 'c'; // sets $b

    $obj->a = 'b'; // sets member variable $a in the object
    $obj->$a = 'c'; // sets member variable $b in the object
    [color=blue]
    > $this->$aboutMenuName s = ...
    > $this->$mainMenuNam es = ...[/color]

    Should be:

    $this->aboutMenuNam es = ...
    $this->mainMenuName s = ...

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Ilija Studen

      #3
      Re: Array Problems

      O.B. wrote:[color=blue]
      > In the code below, the aboutMenuNames array gets overwritten with the
      > values in mainMenuNames in the constructor. What am I doing wrong?
      >
      >
      > class dynamicMenu {
      >
      > var $mainMenuNames;
      >
      > var $aboutMenuNames ;
      >
      > //
      > // Constructor
      > //
      > function dynamicMenu() {
      > $this->$aboutMenuName s = array("About Us",
      > "Statement of Faith",
      > "About Our Pastor",
      > "Directions to Us",
      > "Contact Us");
      >
      > $this->$mainMenuNam es = array("Home",
      > "AboutUs",
      > "ForAdults" ,
      > "ForTheKids ",
      > "ForTheYout h",
      > "WantToServ e");
      >
      >
      > }
      > };
      >[/color]

      Instead of:
      $this->$aboutMenuName s
      $this->$mainMenuNam es

      Write:
      $this->aboutMenuNam es
      $this->mainMenuName s

      --

      Comment

      Working...