Returning a reference to a class variable

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

    Returning a reference to a class variable

    I have a class which contains (1) a class variable and (2) a method
    (e.g., methodA) which returns a reference to the class variable. If
    the variable has been assigned a value by a constant initializer in
    the "var" statement, my httpd process crashes with a segmentation
    fault when I call methodA. If the variable is assigned a value in one
    of the class methods, the httpd process does not crash when methodA is
    called and the value returned is the expected value. This is true
    even if the variable was first initialized in the "var" statement, as
    long as its value is subsequently set in a class method.

    Fails:
    class C {
    var $v = "hello";

    function C () {
    }

    function &methodA () {
    return ($this->v);
    }
    }

    Succeeds:
    class C {
    var $v = "hello";

    function C () {
    $this->v = "hello";
    }

    function &methodA () {
    return ($this->v);
    }
    }

    (Please note that my test case is too complicated to post. I included
    the above examples to illustrate my point, but have not tested them.)

    Has anyone else seen this behavior? Any insight would be appreciated.
  • .bucho

    #2
    Re: Returning a reference to a class variable

    from php manual:

    In PHP 4, only constant initializers for "var" variables are allowed. To
    initialize variables with non-constant values, you need an initialization
    function which is called automatically when an object is being constructed
    from the class. Such a function is called a constructor.


    "nielson" <nielson@spacel ines.com> wrote in message
    news:f40675c.03 09241003.1af233 5b@posting.goog le.com...[color=blue]
    > I have a class which contains (1) a class variable and (2) a method
    > (e.g., methodA) which returns a reference to the class variable. If
    > the variable has been assigned a value by a constant initializer in
    > the "var" statement, my httpd process crashes with a segmentation
    > fault when I call methodA. If the variable is assigned a value in one
    > of the class methods, the httpd process does not crash when methodA is
    > called and the value returned is the expected value. This is true
    > even if the variable was first initialized in the "var" statement, as
    > long as its value is subsequently set in a class method.
    >
    > Fails:
    > class C {
    > var $v = "hello";
    >
    > function C () {
    > }
    >
    > function &methodA () {
    > return ($this->v);
    > }
    > }
    >
    > Succeeds:
    > class C {
    > var $v = "hello";
    >
    > function C () {
    > $this->v = "hello";
    > }
    >
    > function &methodA () {
    > return ($this->v);
    > }
    > }
    >
    > (Please note that my test case is too complicated to post. I included
    > the above examples to illustrate my point, but have not tested them.)
    >
    > Has anyone else seen this behavior? Any insight would be appreciated.[/color]


    Comment

    • nielson

      #3
      Re: Returning a reference to a class variable

      Thanks for the response, but my question was not how to initialize a
      class variable, but why returning a reference to a class variable
      causes my httpd process to crash if the variable was assigned a value
      only in a "var" statement. If the variable was assigned a value in a
      class method, all executes without error and as expected. Has anyone
      else seen similar behavior?

      ".bucho" <dotbucho@comtv .ru> wrote in message news:<bksovt$2q tt$1@bucho.lan> ...[color=blue]
      > from php manual:
      >
      > In PHP 4, only constant initializers for "var" variables are allowed. To
      > initialize variables with non-constant values, you need an initialization
      > function which is called automatically when an object is being constructed
      > from the class. Such a function is called a constructor.
      >
      >
      > "nielson" <nielson@spacel ines.com> wrote in message
      > news:f40675c.03 09241003.1af233 5b@posting.goog le.com...[color=green]
      > > I have a class which contains (1) a class variable and (2) a method
      > > (e.g., methodA) which returns a reference to the class variable. If
      > > the variable has been assigned a value by a constant initializer in
      > > the "var" statement, my httpd process crashes with a segmentation
      > > fault when I call methodA. If the variable is assigned a value in one
      > > of the class methods, the httpd process does not crash when methodA is
      > > called and the value returned is the expected value. This is true
      > > even if the variable was first initialized in the "var" statement, as
      > > long as its value is subsequently set in a class method.
      > >
      > > Fails:
      > > class C {
      > > var $v = "hello";
      > >
      > > function C () {
      > > }
      > >
      > > function &methodA () {
      > > return ($this->v);
      > > }
      > > }
      > >
      > > Succeeds:
      > > class C {
      > > var $v = "hello";
      > >
      > > function C () {
      > > $this->v = "hello";
      > > }
      > >
      > > function &methodA () {
      > > return ($this->v);
      > > }
      > > }
      > >
      > > (Please note that my test case is too complicated to post. I included
      > > the above examples to illustrate my point, but have not tested them.)
      > >
      > > Has anyone else seen this behavior? Any insight would be appreciated.[/color][/color]

      Comment

      Working...