Why can't I reference a member variable?

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

    Why can't I reference a member variable?

    Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to OOP
    in general. I've spent the last three hours trying to figure out why this
    code snipped doesn't do what I expect it to:

    ---snip---
    <?php

    $myTest = new Test;

    echo $myTest->GetClassName ; // Nada

    class Test
    {

    // Class constructor
    function Test()
    {
    // Empty on purpose...
    }

    // Returns class name
    function GetClassName()
    {
    return "Test";
    }

    }

    ?>
    ---snip---

    I would expect the "echo ..." line to display "Test", but it doesn't. I
    swear I must be missing something really simple and really stupid, but I
    can't find it for the life of me. Little help? Oh, PHP Version 4.3.4 if it
    makes a difference.

    Thanks in advance,
    'Mutt
  • MyOdd

    #2
    Re: Why can't I reference a member variable?


    "Shuttermut t" <anonymous@nosp am.net> wrote in message
    news:OpS5c.7856 7$LD5.18060@twi ster.rdc-kc.rr.com...[color=blue]
    > Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to[/color]
    OOP[color=blue]
    > in general. I've spent the last three hours trying to figure out why this
    > code snipped doesn't do what I expect it to:
    >[/color]

    Try it the other way around.
    [color=blue]
    > <?php
    > class Test
    > {
    >
    > // Class constructor
    > function Test()
    > {
    > // Empty on purpose...
    > }
    >
    > // Returns class name
    > function GetClassName()
    > {
    > return "Test";
    > }
    >
    > }[/color]
    [color=blue]
    > $myTest = new Test;
    >
    > echo $myTest->GetClassName ; // Nada
    >
    >
    > ?>[/color]

    Note the class is b4 the calling of the class.

    Simon.


    Comment

    • Bruno Desthuilliers

      #3
      Re: Why can't I reference a member variable?

      Shuttermutt wrote:[color=blue]
      > Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to OOP
      > in general. I've spent the last three hours trying to figure out why this
      > code snipped doesn't do what I expect it to:
      >
      > ---snip---
      > <?php
      >
      > $myTest = new Test;
      >
      > echo $myTest->GetClassName ; // Nada
      >
      > class Test
      > {
      >
      > // Class constructor
      > function Test()
      > {
      > // Empty on purpose...
      > }
      >
      > // Returns class name
      > function GetClassName()
      > {
      > return "Test";
      > }
      >
      > }
      >
      > ?>
      > ---snip---
      >
      > I would expect the "echo ..." line to display "Test", but it doesn't. I
      > swear I must be missing something really simple and really stupid, but I
      > can't find it for the life of me. Little help? Oh, PHP Version 4.3.4 if it
      > makes a difference.[/color]

      Raise your error level, and you'll have the answer (which is quite
      obvious BTW, but warnings may help spot less obvious bugs...)

      Notice: Undefined property: GetClassName in <filename>.ph p on line 5

      Ok, line 5 is :
      echo $myTest->GetClassName ; // Nada

      Err... seems like parens are missings !-) Used to be a VB coder ?-)

      Try this instead :
      echo $myTest->GetClassName() ;
      [color=blue]
      > Thanks in advance,[/color]
      You're welcome

      Comment

      • Bruno Desthuilliers

        #4
        Re: Why can't I reference a member variable?

        MyOdd wrote:[color=blue]
        > "Shuttermut t" <anonymous@nosp am.net> wrote in message
        > news:OpS5c.7856 7$LD5.18060@twi ster.rdc-kc.rr.com...
        >[color=green]
        >>Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to[/color]
        >
        > OOP
        >[color=green]
        >>in general. I've spent the last three hours trying to figure out why this
        >>code snipped doesn't do what I expect it to:
        >>[/color]
        >
        >
        > Try it the other way around.
        >
        >[color=green]
        >><?php
        >> class Test
        >> {
        >>
        >> // Class constructor
        >> function Test()
        >> {
        >> // Empty on purpose...
        >> }
        >>
        >> // Returns class name
        >> function GetClassName()
        >> {
        >> return "Test";
        >> }
        >>
        >> }[/color]
        >
        >[color=green]
        >>$myTest = new Test;
        >>
        >>echo $myTest->GetClassName ; // Nada
        >>
        >>
        >>?>[/color]
        >
        >
        > Note the class is b4 the calling of the class.[/color]

        Err... The fact is that
        - your code exhibits the same problem (you didn't tried your 'solution',
        did you ?)
        - the OP's code, once the *real* problem is corrected (see other post in
        this thread), works quite ok.

        BTW, I don't know much about the internals of PHP, but it seems that the
        whole file is parsed before execution, so you don't need to put the
        class definition *before* using it.

        Bruno

        Comment

        • MyOdd

          #5
          Re: Why can't I reference a member variable?

          [color=blue][color=green]
          > >
          > >[color=darkred]
          > >>$myTest = new Test;
          > >>
          > >>echo $myTest->GetClassName ; // Nada
          > >>
          > >>
          > >>?>[/color]
          > >
          > >
          > > Note the class is b4 the calling of the class.[/color]
          >
          > Err... The fact is that
          > - your code exhibits the same problem (you didn't tried your 'solution',
          > did you ?)[/color]

          No, sorry.
          [color=blue]
          > - the OP's code, once the *real* problem is corrected (see other post in
          > this thread), works quite ok.[/color]

          Ideed.
          [color=blue]
          >
          > BTW, I don't know much about the internals of PHP, but it seems that the
          > whole file is parsed before execution, so you don't need to put the
          > class definition *before* using it.
          >[/color]

          Not for me, but i don't feel like looking into it.

          Simon


          Comment

          • Shuttermutt

            #6
            Re: Why can't I reference a member variable?

            Bruno Desthuilliers wrote:
            [color=blue]
            >
            > Err... seems like parens are missings !-) Used to be a VB coder ?-)
            >
            > Try this instead :
            > echo $myTest->GetClassName() ;
            >[color=green]
            >> Thanks in advance,[/color]
            > You're welcome[/color]

            I am SUCH an idiot! Thank you.

            --
            'Mutt

            Comment

            • Chung Leong

              #7
              Re: Why can't I reference a member variable?

              Uzytkownik "Bruno Desthuilliers" <bdesth.quelque chose@free.quel quepart.fr>
              napisal w wiadomosci news:405830f9$0 $278$626a14ce@n ews.free.fr...[color=blue]
              > BTW, I don't know much about the internals of PHP, but it seems that the
              > whole file is parsed before execution, so you don't need to put the
              > class definition *before* using it.[/color]

              No, in PHP class definition is a runtime operation. That's why something
              like this works:

              function define_cow($goo d) {
              if($good) {
              class Cow {
              var $name;
              var $weight;
              }
              }
              else {
              class Cow {
              var $name;
              var $madness_severi ty;
              }
              }
              }

              define_cow(fals e);
              $obj = new Cow();


              Comment

              • Lāʻie Techie

                #8
                Re: Why can't I reference a member variable?

                On Wed, 17 Mar 2004 12:30:16 +0100, Bruno Desthuilliers wrote:
                [color=blue]
                > MyOdd wrote:
                > BTW, I don't know much about the internals of PHP, but it seems that the
                > whole file is parsed before execution, so you don't need to put the class
                > definition *before* using it.
                >
                > Bruno[/color]

                That has changed as of PHP 5.

                La'ie Techie

                Comment

                Working...