PHP Classes

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

    PHP Classes

    This is sort of tied in with the question I asked earlier. Let me know
    if I'm going insane, because I could swear classes are supposed to work
    this way in other languages.

    Let's say we have class Counter. It's declared like this.

    class Counter
    {
    var $counter = 0;

    function count()
    {
    $this->counter++;
    }
    }

    //and class Test

    class Test
    {
    function tester($counter ) // receives counter object
    {
    $counter->count();
    }
    }

    //Now, let's say in the main part of the program it works like this:

    $counter = new Counter();
    $test = new Test();

    echo $counter->counter; // should echo 0, does print out 0
    $test->tester($counte r);
    echo $counter->counter; // should echo 1, actually prints out 0


    Why doesn't this work? I'm sure it works in C#. If this is the way it's
    supposed to work in PHP, I believe it's fairly silly. Is there a way to
    hack it into working? I suppose I could use globals, but I'd really
    rather not. Does PHP5 change this?

    Thanks

    ~ sock
  • Chris Hope

    #2
    Re: PHP Classes

    erotic sock wrote:
    [color=blue]
    > This is sort of tied in with the question I asked earlier. Let me know
    > if I'm going insane, because I could swear classes are supposed to work
    > this way in other languages.
    >
    > Let's say we have class Counter. It's declared like this.
    >
    > class Counter
    > {
    > var $counter = 0;
    >
    > function count()
    > {
    > $this->counter++;
    > }
    > }
    >
    > //and class Test
    >
    > class Test
    > {
    > function tester($counter ) // receives counter object
    > {
    > $counter->count();
    > }
    > }
    >
    > //Now, let's say in the main part of the program it works like this:
    >
    > $counter = new Counter();
    > $test = new Test();
    >
    > echo $counter->counter; // should echo 0, does print out 0
    > $test->tester($counte r);
    > echo $counter->counter; // should echo 1, actually prints out 0
    >
    >
    > Why doesn't this work? I'm sure it works in C#. If this is the way it's
    > supposed to work in PHP, I believe it's fairly silly. Is there a way to
    > hack it into working? I suppose I could use globals, but I'd really
    > rather not. Does PHP5 change this?[/color]

    If I recall correctly, PHP4 (and earlier) pass objects by value and PHP5
    passes objects by reference. So in the 'Test' class you are modifying the
    property of a passed in value, and not the original value. To make it do
    what you are expecting you need to explicitly pass in the object in by
    reference by either putting an & in front of the variable like so:

    function tester(&$counte r)
    {
    $counter->count();
    }

    or else when you call the function like so:

    $test->tester(&$count er);

    --
    Chris Hope
    The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • erotic sock

      #3
      Re: PHP Classes

      Chris Hope wrote:[color=blue]
      > erotic sock wrote:[color=green]
      >><snip my question>[/color]
      >
      > If I recall correctly, PHP4 (and earlier) pass objects by value and PHP5
      > passes objects by reference. So in the 'Test' class you are modifying the
      > property of a passed in value, and not the original value. To make it do
      > what you are expecting you need to explicitly pass in the object in by
      > reference by either putting an & in front of the variable like so:
      >
      > function tester(&$counte r)
      > {
      > $counter->count();
      > }
      >
      > or else when you call the function like so:
      >
      > $test->tester(&$count er);
      >[/color]

      Thanks for that. I didn't realise the & modifier worked.

      ~ sock

      Comment

      Working...