Problem with classes/constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martien van Wanrooij

    #1

    Problem with classes/constructors

    I am rather new at the classes concept in php so maybe I am overseeing
    something very simple but anyway I tried the following code:
    <?php
    class Navbar
    {
    var $begin = "[";
    var $tussen = "][";
    var $einde = "]";
    function Navbar($bBegin = "[", $bTussen ="][", $bEinde ="]")
    {
    $this->begin = $bBegin;
    $this->tussen = $bTussen;
    $this->einde = $bEinde;
    }
    function openen($item)
    {
    print $begin.$item;
    }
    function addItem($item)
    {
    print $tussen.$item;
    }
    function sluiten($item)
    {
    print $tussen.$item.$ einde;
    }
    function test()
    {
    echo $begin;
    }
    }
    $navBar1 = new Navbar("[","][","]");
    $navBar1->openen("Item1" );
    $navBar1->addItem("Item2 ");
    $navBar1->addItem("Item3 ");
    $navBar1->sluiten("Item4 ");
    $navBar1->test();
    ?>
    The problem is that when I see the page online, the separators (in this case
    [ ][ ] ) are not seen. Obviously the variables $begin, $tussen and $einde
    are empty.
    Any suggestions?

    Martien


  • Alvaro G Vicario

    #2
    Re: Problem with classes/constructors

    *** Martien van Wanrooij wrote/escribió (Fri, 4 Jun 2004 12:30:56 +0200):[color=blue]
    > function test()
    > {
    > echo $begin;
    > }[/color]

    Since $begin is not defined within test() it will be empty. If you are
    referring to the class attribute you have to call it this way:

    function test()
    {
    echo $this->begin;
    }

    You did it fine in constructor.

    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --

    Comment

    • Martien van Wanrooij

      #3
      Re: Problem with classes/constructors

      Thank you very much Alvaro! This seems to work a little bit different from
      Java (as far as I recall anyway) but it is completely clear now.

      Martien.


      Comment

      Working...