Pointer in PHP

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

    Pointer in PHP

    Hi,

    Is it possible to keep a pointer to an object in a PHP variable? I'd like to
    do this :

    $obj1 = new myClass(...);
    $obj2 = $obj1;
    $obj1->name = "foo";
    $obj2->name = "bar";

    echo $obj1->name;

    Which would display "bar" instead of "foo" with the current PHP behavior.

    Thanks a lot,
    Denis




  • rush

    #2
    Re: Pointer in PHP

    "Denis Crespe" <a@a.com> wrote in message
    news:bnh2mc$1p2 $1@news.tiscali .fr...[color=blue]
    > $obj1 = new myClass(...);
    > $obj2 = $obj1;
    > $obj1->name = "foo";
    > $obj2->name = "bar";
    >
    > echo $obj1->name;
    >
    > Which would display "bar" instead of "foo" with the current PHP behavior.[/color]

    you can not get the pointer, but you can get the reference:

    $obj2 = & $obj1;

    rush
    --




    Comment

    • Denis Crespe

      #3
      Re: Pointer in PHP

      Awesome! It's what I wanted :-)

      Denis

      "rush" <pipa@rush.aval on.hr> a écrit dans le message de news:
      bnh4b3$e4u$1@ls 219.htnet.hr...[color=blue]
      > "Denis Crespe" <a@a.com> wrote in message
      > news:bnh2mc$1p2 $1@news.tiscali .fr...[color=green]
      > > $obj1 = new myClass(...);
      > > $obj2 = $obj1;
      > > $obj1->name = "foo";
      > > $obj2->name = "bar";
      > >
      > > echo $obj1->name;
      > >
      > > Which would display "bar" instead of "foo" with the current PHP[/color][/color]
      behavior.[color=blue]
      >
      > you can not get the pointer, but you can get the reference:
      >
      > $obj2 = & $obj1;
      >
      > rush
      > --
      > http://www.templatetamer.com/
      >
      >
      >[/color]


      Comment

      • John Downey

        #4
        Re: Pointer in PHP

        Denis Crespe wrote:[color=blue]
        > Hi,
        >
        > Is it possible to keep a pointer to an object in a PHP variable? I'd like to
        > do this :
        >
        > $obj1 = new myClass(...);
        > $obj2 = $obj1;
        > $obj1->name = "foo";
        > $obj2->name = "bar";
        >
        > echo $obj1->name;
        >
        > Which would display "bar" instead of "foo" with the current PHP behavior.
        >
        > Thanks a lot,
        > Denis[/color]
        $obj2 = &$obj1;

        --
        John Downey




        Comment

        Working...