Need to return reference in this case?

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

    Need to return reference in this case?

    a simple singleton class (PHP4)

    which way is preffered?

    // 1.
    class Foo {

    function getFoo() {
    static $instace;
    if (!isset($instac e) ) {
    $instance = new Foo();
    // ...
    }
    return $instance;
    }
    }

    $foo = Foo::getFoo();

    // 2.

    class Foo {

    function &getFoo() {
    static $instace;
    if (!isset($instac e) ) {
    $instance = new Foo();
    // ...
    }
    return $instance;
    }
    }

    $foo =& Foo::getFoo();

  • gosha bine

    #2
    Re: Need to return reference in this case?

    howa wrote:
    a simple singleton class (PHP4)
    >
    which way is preffered?
    >
    // 1.
    class Foo {
    >
    function getFoo() {
    static $instace;
    if (!isset($instac e) ) {
    $instance = new Foo();
    // ...
    }
    return $instance;
    }
    }
    >
    $foo = Foo::getFoo();
    >
    // 2.
    >
    class Foo {
    >
    function &getFoo() {
    static $instace;
    if (!isset($instac e) ) {
    $instance = new Foo();
    // ...
    }
    return $instance;
    }
    }
    >
    $foo =& Foo::getFoo();
    >

    --
    gosha bine

    extended php parser ~ http://code.google.com/p/pihipi
    blok ~ http://www.tagarga.com/blok

    Comment

    • gosha bine

      #3
      Re: Need to return reference in this case?

      howa wrote:
      a simple singleton class (PHP4)
      >
      which way is preffered?
      >
      // 1.
      class Foo {
      >
      function getFoo() {
      static $instace;
      if (!isset($instac e) ) {
      $instance = new Foo();
      // ...
      }
      return $instance;
      }
      }
      >
      $foo = Foo::getFoo();
      >
      // 2.
      >
      class Foo {
      >
      function &getFoo() {
      static $instace;
      if (!isset($instac e) ) {
      $instance = new Foo();
      // ...
      }
      return $instance;
      }
      }
      >
      $foo =& Foo::getFoo();
      >
      The second. "foo = new Foo" will create two copies of "Foo" in php4.

      Any reason why you can't use php5?

      --
      gosha bine

      extended php parser ~ http://code.google.com/p/pihipi
      blok ~ http://www.tagarga.com/blok

      Comment

      • howa

        #4
        Re: Need to return reference in this case?

        On Jun 2, 7:51 pm, gosha bine <stereof...@gma il.comwrote:
        The second. "foo = new Foo" will create two copies of "Foo" in php4.
        >
        what do you mean? you mean the second one is preferred?

        why the first one will create two copies of "Foo" but not the first
        one?

        Any reason why you can't use php5?
        >
        php4 is stable, and its OOP function is enough (and simple) as far as
        known.

        Thanks.

        Comment

        • Schraalhans Keukenmeester

          #5
          Re: Need to return reference in this case?

          At Sat, 02 Jun 2007 06:44:46 -0700, howa let h(is|er) monkeys type:
          On Jun 2, 7:51 pm, gosha bine <stereof...@gma il.comwrote:
          >The second. "foo = new Foo" will create two copies of "Foo" in php4.
          >>
          >
          what do you mean? you mean the second one is preferred?
          >
          why the first one will create two copies of "Foo" but not the first
          one?
          >
          >
          >Any reason why you can't use php5?
          >>
          >
          php4 is stable, and its OOP function is enough (and simple) as far as
          known.
          >
          Thanks.
          Stable, ok. OOP = enough, I beg to differ. OOP in PHP4 is poor, if not
          cr@pola. PHP5 unstable? (which I think you imply by your answer to Gosha's
          question) Why? How? When?

          I get that typical "argument without foundation" feeling....

          Sh.
          --
          Schraalhans Keukenmeester - schraalhans@the .Spamtrapexampl e.nl
          [Remove the lowercase part of Spamtrap to send me a message]

          "strcmp('apples ','oranges') < 0"

          Comment

          • Michael Fesser

            #6
            Re: Need to return reference in this case?

            ..oO(howa)
            >Any reason why you can't use php5?
            >
            >php4 is stable
            So is PHP 5.
            >and its OOP function is enough (and simple) as far as
            >known.
            OOP in PHP 4 is a joke, I wouldn't even call it OOP. An object in PHP 4
            is just an ugly hack, an array with some attached functions. In PHP 5
            you get an idea of what OOP really means and what you can do with it.

            I don't see any reason to stay with PHP 4 anymore, it's outdated.

            Micha

            Comment

            • howa

              #7
              Re: Need to return reference in this case?

              On 6 3 , 12 06 , Schraalhans Keukenmeester
              <Schraalh...@th e.spamtrapexamp le.nlwrote:
              Stable, ok. OOP = enough, I beg to differ. OOP in PHP4 is poor, if not
              cr@pola. PHP5 unstable? (which I think you imply by your answer to Gosha's
              question) Why? How? When?
              >
              I get that typical "argument without foundation" feeling....
              >
              Hello,

              this is a little out of topic...i don't want to start a war on PHP4 vs
              PHP5 :)

              anyway, my $0.02...

              1. if it ain't broken, don't fix it. just like many large web sites
              are still using PHP4, Apache 1.x, MySQL 4.x etc ...


              2. personally, i think PHP5 OOP is not a well developed, maybe there
              will be great changes in PHP6, even we can move to PHP6 directly
              somedays...but now, PHP 4 works quite well.


              Comment

              • howa

                #8
                Re: Need to return reference in this case?

                On 6 3 , 12 52 , Michael Fesser <neti...@gmx.de wrote:
                >
                OOP in PHP 4 is a joke, I wouldn't even call it OOP. An object in PHP 4
                is just an ugly hack, an array with some attached functions. In PHP 5
                you get an idea of what OOP really means and what you can do with it.
                >
                i think the point is : there is always more than one way to do it

                even you talk to wordpress developers, they even hesitate to use OOP.

                array with some attached functions worked in our setup, that's nothing
                wrong

                :)



                Comment

                • Jerry Stuckle

                  #9
                  Re: Need to return reference in this case?

                  howa wrote:
                  On 6 3 , 12 06 , Schraalhans Keukenmeester
                  <Schraalh...@th e.spamtrapexamp le.nlwrote:
                  >
                  >Stable, ok. OOP = enough, I beg to differ. OOP in PHP4 is poor, if not
                  >cr@pola. PHP5 unstable? (which I think you imply by your answer to Gosha's
                  >question) Why? How? When?
                  >>
                  >I get that typical "argument without foundation" feeling....
                  >>
                  >
                  Hello,
                  >
                  this is a little out of topic...i don't want to start a war on PHP4 vs
                  PHP5 :)
                  >
                  anyway, my $0.02...
                  >
                  1. if it ain't broken, don't fix it. just like many large web sites
                  are still using PHP4, Apache 1.x, MySQL 4.x etc ...
                  >
                  >
                  2. personally, i think PHP5 OOP is not a well developed, maybe there
                  will be great changes in PHP6, even we can move to PHP6 directly
                  somedays...but now, PHP 4 works quite well.
                  >
                  >
                  If that were the case, we'd still be programming in assembler, cobol and
                  rpg.

                  I agree with the others. OOP support in PHP4 is terrible. That in
                  itself is enough reason to upgrade. And only laziness prevents someone
                  from upgrading on their own server.

                  Shared hosting is a little different. There are some differences which
                  can break PHP4 scripts, especially poorly written ones. And to force
                  your customers to rewrite their scripts is not necessarily good.

                  It has nothing to to with "good enough".

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • gosha bine

                    #10
                    Re: Need to return reference in this case?

                    howa wrote:
                    On Jun 2, 7:51 pm, gosha bine <stereof...@gma il.comwrote:
                    >The second. "foo = new Foo" will create two copies of "Foo" in php4.
                    >>
                    >
                    what do you mean? you mean the second one is preferred?
                    >
                    why the first one will create two copies of "Foo" but not the first
                    one?
                    >
                    $foo = new Foo

                    does the following in php4:

                    1) a chunk of memory is allocated and object's constructor is called to
                    initialize this memory
                    2) another chunk of memory is allocated and everything from the first
                    chunk is copied to it
                    3) variable 'foo' is being set to address of this second chunk
                    4) the first chunk is being disposed.

                    Apart from being ineffective, this technique will apparently fail if an
                    object saves reference to itself ($this) in constructor or when you need
                    exactly one instance.
                    >
                    >Any reason why you can't use php5?
                    >>
                    >
                    php4 is stable, and its OOP function is enough (and simple) as far as
                    known.
                    >
                    I agree that OO "features" of php5 are mostly useless and php4 has
                    everything one needs to write OO programs. However, I still won't
                    recommend it, exactly for the reason described above.

                    --
                    gosha bine

                    extended php parser ~ http://code.google.com/p/pihipi
                    blok ~ http://www.tagarga.com/blok

                    Comment

                    • howa

                      #11
                      Re: Need to return reference in this case?

                      On 6 3 , 6 42 , gosha bine <stereof...@gma il.comwrote:
                      howa wrote:
                      $foo = new Foo
                      >
                      does the following in php4:
                      >
                      1) a chunk of memory is allocated and object's constructor is called to
                      initialize this memory
                      2) another chunk of memory is allocated and everything from the first
                      chunk is copied to it
                      3) variable 'foo' is being set to address of this second chunk
                      4) the first chunk is being disposed.
                      >
                      Apart from being ineffective, this technique will apparently fail if an
                      object saves reference to itself ($this) in constructor or when you need
                      exactly one instance.
                      >
                      >
                      Hello,

                      Yes, but refer to my original question, both (1) & (2) have this
                      problem of inefficieny.

                      So you said (2) is perfferred?

                      Why? Since PHP said return by reference is mostly useless.


                      I agree that OO "features" of php5 are mostly useless and php4 has
                      everything one needs to write OO programs. However, I still won't
                      recommend it, exactly for the reason described above.
                      >
                      This is not a big problem in fact, people can upgrade to PHP5, but
                      still using PHP4 syntax. We don't like PHP5 since it is not well
                      planned, and subject to change.

                      Comment

                      • mypetprogrammer@gmail.com

                        #12
                        Re: Need to return reference in this case?

                        On Jun 2, 11:14 pm, howa <howac...@gmail .comwrote:
                        On 6 3 , 6 42 , gosha bine <stereof...@gma il.comwrote:
                        >
                        >
                        >
                        howa wrote:
                        $foo = new Foo
                        >
                        does the following in php4:
                        >
                        1) a chunk of memory is allocated and object's constructor is called to
                        initialize this memory
                        2) another chunk of memory is allocated and everything from the first
                        chunk is copied to it
                        3) variable 'foo' is being set to address of this second chunk
                        4) the first chunk is being disposed.
                        >
                        Apart from being ineffective, this technique will apparently fail if an
                        object saves reference to itself ($this) in constructor or when you need
                        exactly one instance.
                        >
                        Hello,
                        >
                        Yes, but refer to my original question, both (1) & (2) have this
                        problem of inefficieny.
                        >
                        So you said (2) is perfferred?
                        >
                        Why? Since PHP said return by reference is mostly useless.
                        >
                        I agree that OO "features" of php5 are mostly useless and php4 has
                        everything one needs to write OO programs. However, I still won't
                        recommend it, exactly for the reason described above.
                        >
                        This is not a big problem in fact, people can upgrade to PHP5, but
                        still using PHP4 syntax. We don't like PHP5 since it is not well
                        planned, and subject to change.
                        That sounds wrong to me. PHP5's OOP is useless only if you don't have
                        any use for OOP in the first place. You want a struct, use PHP4 OOP,
                        fine. But if you want actual OOP, use PHP5. I know I couldn't use PHP4
                        for the database framework I wrote in 5 based on an abstract
                        superclass.

                        ~A!

                        Comment

                        • gosha bine

                          #13
                          Re: Need to return reference in this case?

                          howa wrote:
                          On 6 3 , 6 42 , gosha bine <stereof...@gma il.comwrote:
                          >howa wrote:
                          >$foo = new Foo
                          >>
                          >does the following in php4:
                          >>
                          >1) a chunk of memory is allocated and object's constructor is called to
                          >initialize this memory
                          >2) another chunk of memory is allocated and everything from the first
                          >chunk is copied to it
                          >3) variable 'foo' is being set to address of this second chunk
                          >4) the first chunk is being disposed.
                          >>
                          >Apart from being ineffective, this technique will apparently fail if an
                          >object saves reference to itself ($this) in constructor or when you need
                          >exactly one instance.
                          >>
                          >>
                          >
                          Hello,
                          >
                          Yes, but refer to my original question, both (1) & (2) have this
                          problem of inefficieny.
                          >
                          So you said (2) is perfferred?
                          >
                          Why? Since PHP said return by reference is mostly useless.
                          function getFoo()...
                          $foo = Foo::getFoo();

                          involves copying as I said above

                          function &getFoo()..
                          $foo =& Foo::getFoo();

                          does not involve copying, only one memory block is created




                          --
                          gosha bine

                          extended php parser ~ http://code.google.com/p/pihipi
                          blok ~ http://www.tagarga.com/blok

                          Comment

                          • Schraalhans Keukenmeester

                            #14
                            Re: Need to return reference in this case?

                            At Sat, 02 Jun 2007 10:28:30 -0700, howa let h(is|er) monkeys type:

                            >
                            i think the point is : there is always more than one way to do it
                            >
                            even you talk to wordpress developers, they even hesitate to use OOP.
                            >
                            With al due respect to them (WP developers), have you ever studied the
                            Wordpress core code? Their hesitation to go OOP seems to be
                            self-explanatory glancing over their stuff. I had visions of the MIR
                            space-station innards. The damn thing worked, but oh boy, you don't want
                            to see inside how it's achieved.

                            But I don't want to start a flamewar here either. I'll agree to disagree.
                            Rgds

                            --
                            Schraalhans Keukenmeester - schraalhans@the .Spamtrapexampl e.nl
                            [Remove the lowercase part of Spamtrap to send me a message]

                            "strcmp('apples ','oranges') < 0"

                            Comment

                            • howa

                              #15
                              Re: Need to return reference in this case?

                              On Jun 3, 6:42 pm, gosha bine <stereof...@gma il.comwrote:
                              howa wrote:
                              On 6 3 , 6 42 , gosha bine <stereof...@gma il.comwrote:
                              howa wrote:
                              $foo = new Foo
                              >
                              does the following in php4:
                              >
                              1) a chunk of memory is allocated and object's constructor is called to
                              initialize this memory
                              2) another chunk of memory is allocated and everything from the first
                              chunk is copied to it
                              3) variable 'foo' is being set to address of this second chunk
                              4) the first chunk is being disposed.
                              >
                              Apart from being ineffective, this technique will apparently fail if an
                              object saves reference to itself ($this) in constructor or when you need
                              exactly one instance.
                              >
                              Hello,
                              >
                              Yes, but refer to my original question, both (1) & (2) have this
                              problem of inefficieny.
                              >
                              So you said (2) is perfferred?
                              >
                              Why? Since PHP said return by reference is mostly useless.
                              >
                              function getFoo()...
                              $foo = Foo::getFoo();
                              >
                              involves copying as I said above
                              >
                              function &getFoo()..
                              $foo =& Foo::getFoo();
                              >
                              does not involve copying, only one memory block is created
                              >
                              Good, back to the real problem!

                              :)

                              but according to PHP, the opimizer is smart to handle this?
                              >>Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own


                              any comments?

                              thanks.


                              Comment

                              Working...