URGENT: variable scope in PHP classes

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

    URGENT: variable scope in PHP classes

    Hi everyone

    I am having some problems with variable scope in my php classes. I thought
    varibles declared in classes were visble throughout the class, but I am
    finding this is not the case.

    What is happening is that when functions in my class go to access variables
    they are returning nothing even though they have had values assigned..

    Any idea what is going on?

    Thanks

    Hamilton


    "the web site for web sites?



  • Antti Lahtonen

    #2
    Re: URGENT: variable scope in PHP classes

    Spidah <hamilton@eyehu g.co.nz> kirjoitti:
    [color=blue]
    > What is happening is that when functions in my class go to access variables
    > they are returning nothing even though they have had values assigned..[/color]

    You must have something wrong in your code. :) An example would have
    helped a lot.

    What do you mean with a variable - a common variable or an attribute?
    Attributes are available in all functions (methods) of the class, but
    common variables only in the functions they are declared in.

    class foo {
    var $bar = 'foobar'; // attribute

    function example() {
    print $this->bar; // prints foobar
    $variable = 'cheese'; // variable
    }

    function example2() {
    print $variable; // prints nothing
    }
    }

    --
    Antti Lahtonen

    Comment

    • Spidah

      #3
      Re: URGENT: variable scope in PHP classes

      I was talking about attributes rather than common variables.

      It is my understanding that, as you stated, they are available to every
      method of the class. I assume this is the case regardless of what method
      assigns the value to that attribute?

      For example:

      class MyClass{

      var $myvar;

      function one (){
      $this->myvar = "foobar";
      }

      function two(){
      echo $this->myvar;
      }

      function three(){
      $temp = "this is a ";
      return $temp . $this->myvar;
      }
      }

      What I am finding as that in some instances two() is sending nothing to the
      browser, even if one() has been called first. If I use myvar in another
      method, in this example three(), between the calls to one() and two(), then
      even though three() contains no code to alter the value of myvar, the
      subsequent call to two() returns nothing.

      I gather I have just done something wrong in my code and that?

      Hamilton


      "Antti Lahtonen" <anlahton@verso .st.jyu.fi> wrote in message
      news:birepp$126 $1@mordred.cc.j yu.fi...[color=blue]
      > Spidah <hamilton@eyehu g.co.nz> kirjoitti:
      >[color=green]
      > > What is happening is that when functions in my class go to access[/color][/color]
      variables[color=blue][color=green]
      > > they are returning nothing even though they have had values assigned..[/color]
      >
      > You must have something wrong in your code. :) An example would have
      > helped a lot.
      >
      > What do you mean with a variable - a common variable or an attribute?
      > Attributes are available in all functions (methods) of the class, but
      > common variables only in the functions they are declared in.
      >
      > class foo {
      > var $bar = 'foobar'; // attribute
      >
      > function example() {
      > print $this->bar; // prints foobar
      > $variable = 'cheese'; // variable
      > }
      >
      > function example2() {
      > print $variable; // prints nothing
      > }
      > }
      >
      > --
      > Antti Lahtonen
      > http://www.iki.fi/andu/[/color]


      Comment

      • Matthew Vickers

        #4
        Re: URGENT: variable scope in PHP classes

        On Mon, 1 Sep 2003 09:21:58 +1200
        "Spidah" <hamilton@eyehu g.co.nz> wrote:

        <snip>
        [color=blue]
        > class MyClass{
        >
        > var $myvar;
        >
        > function one (){
        > $this->myvar = "foobar";
        > }
        >
        > function two(){
        > echo $this->myvar;
        > }
        >
        > function three(){
        > $temp = "this is a ";
        > return $temp . $this->myvar;
        > }
        > }
        >
        > What I am finding as that in some instances two() is sending nothing
        > to the browser, even if one() has been called first. If I use myvar in
        > another method, in this example three(), between the calls to one()
        > and two(), then even though three() contains no code to alter the
        > value of myvar, the subsequent call to two() returns nothing.
        >
        > I gather I have just done something wrong in my code and that?
        >[/color]

        <snip>

        There seems to be nothing wrong with this class. I have run the code
        here and all is fine. The output is what one would expect. You may
        have a problem in your real class.

        Matt


        --
        Quispiam Power Computing | "There are two major products that come out
        Pendle Hill, Australia | of Berkeley: LSD and UNIX. We don't believe
        +61 2 9631 7719 | this to be a coincidence. "
        www.quispiam.com | - Jeremy S. Anderson

        Comment

        • Antti Lahtonen

          #5
          Re: URGENT: variable scope in PHP classes

          Spidah <hamilton@eyehu g.co.nz> kirjoitti:
          [color=blue]
          > It is my understanding that, as you stated, they are available to every
          > method of the class. I assume this is the case regardless of what method
          > assigns the value to that attribute?[/color]

          Yes. Of course the method has to be called first.
          [color=blue]
          > For example:
          > [..]
          > I gather I have just done something wrong in my code and that?[/color]

          The class seems to be ok. Probably you have an error somewhere where you
          are calling instances of that class.

          Remember to use:
          error_reporting (E_ALL);
          as the first line of your code - it helps finding problems related to
          empty variables.


          --
          Antti Lahtonen

          Comment

          Working...