Php two colons " :: " syntax or naming convention ?

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

    Php two colons " :: " syntax or naming convention ?

    I see two colons in the middle of what look like method calls here and
    there.
    Such as when using PEAR

    // implement pear db object
    $this->_oConn =& DB::connect(DSN );

    I have looked all over the place and have been unable to figure out
    exactly what the :: means or does ? It does not appear to explain it
    in Programming PHP by O'Reilly, or I have been unable to find it in
    the book as well

    Any help would be appreciated.
  • Daniel Tryba

    #2
    Re: Php two colons " :: " syntax or naming convention ?

    Charon <eric.bauld@edu .sait.ab.ca> wrote:[color=blue]
    > $this->_oConn =& DB::connect(DSN );
    >
    > I have looked all over the place and have been unable to figure out
    > exactly what the :: means or does ? It does not appear to explain it
    > in Programming PHP by O'Reilly, or I have been unable to find it in
    > the book as well[/color]

    It's in the reference under the "Classes and Objects" section:
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    It's a static accessor into objects.

    --

    Daniel Tryba

    Comment

    • Johan Holst Nielsen

      #3
      Re: Php two colons &quot; :: &quot; syntax or naming convention ?

      Charon wrote:[color=blue]
      > I see two colons in the middle of what look like method calls here and
      > there.
      > Such as when using PEAR
      >
      > // implement pear db object
      > $this->_oConn =& DB::connect(DSN );
      >
      > I have looked all over the place and have been unable to figure out
      > exactly what the :: means or does ? It does not appear to explain it
      > in Programming PHP by O'Reilly, or I have been unable to find it in
      > the book as well
      >
      > Any help would be appreciated.[/color]

      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


      :)

      Regards,
      Johan

      Comment

      • Marcus Baker

        #4
        Re: Php two colons &quot; :: &quot; syntax or naming convention ?

        Hi...

        Hi...Charon wrote:[color=blue]
        > I have looked all over the place and have been unable to figure out
        > exactly what the :: means or does ?[/color]

        It's a direct function call to the class. You don't need to create an
        object to make this call. A simpler example would be...

        class Thing {
        function doStuff() {
        }
        }

        I can either create an object and make the call...

        $thing = &new Thing();
        $thing->doStuff();

        ....or I could call it directly...

        Thing::doStuff( );

        Here the method is invoked without any Thing object being created in
        memory. The catch is that the $this variable will not be available when
        we make this type of call. If the class did this...

        class Thing {
        var $_message;

        function doStuff() {
        print $this->_message;
        }
        }

        The second type of invocation would fail as $this is undefined.

        The :: type of call is called a "static" invocation or "static
        dispatch". When we do Thing::doStuff( ) we know exactly what piece of
        code will execute. It will never change.

        Suppose we have a class Thang that extends Thing...

        class Thang extends Thing { ... }

        $thing = &new Thang();
        $thing->doStuff();

        Do we know that the above code was executed. No we don't because Thang
        might have overridden it. This is a "virtual" invocation. It looks like
        a method call, but can be switched within the class hierarchy.

        A method that is designed to be called statically (so it has no $this
        inside) is often called a "static method". They are a lot less flexible
        than the virtual dispatch mechanism, so use them wisely.

        In fact the PEAR library is a good example of when not to use static
        methods. They are used for error handling. Error handling is usually an
        application responsibility and should not be dictated by the library.
        Extending errors in PEAR packages usually involves unecessary digging in
        source code so that they can be reliably wrapped.

        yours, Marcus
        --
        Marcus Baker, marcus@lastcraf t.com, nokia@appo.demo n.co.uk

        Comment

        • Christian Fersch

          #5
          Re: Re: Php two colons &quot; :: &quot; syntax or naming convention ?

          > The :: type of call is called a "static" invocation or "static[color=blue]
          > dispatch". When we do Thing::doStuff( ) we know exactly what piece of
          > code will execute. It will never change.[/color]

          not true :P
          look at this:

          if ($a){
          class a {
          function show(){
          echo 'this was a';
          }
          }
          } else {
          class a {
          function show(){
          echo 'this was not a';
          }
          }
          }

          a::show();

          ;)

          --
          mfg Christian (Chronial "at" web.de)

          --
          Composed with Newz Crawler 1.5 http://www.newzcrawler.com/

          Comment

          • Marcus Baker

            #6
            Re: Php two colons &quot; :: &quot; syntax or naming convention ?

            Hi...

            Christian Fersch wrote:[color=blue][color=green]
            >>The :: type of call is called a "static" invocation or "static
            >>dispatch". When we do Thing::doStuff( ) we know exactly what piece of
            >>code will execute. It will never change.[/color][/color]

            Doh! Confused myself :P.
            [color=blue]
            >
            >
            > not true :P
            > look at this:[/color]

            I had in my head a different scenario. One where a method does this...

            class ClientCode {
            function doStuff() {
            Thing::doStuff( );
            }
            }

            ....rather than this...

            class ClientCode {
            function doStuff(&$thing ) {
            $thing->doStuff();
            }
            }

            Must remember not to post late at night :(.

            yours, Marcus
            --
            Marcus Baker, marcus@lastcraf t.com, nokia@appo.demo n.co.uk

            Comment

            • Charon

              #7
              Re: Php two colons &quot; :: &quot; syntax or naming convention ?

              Quite informative and what I was looking for. Including the other two posts.
              But would you clarify the mistake that was made ?
              I understood the example by Christian as a way to break the method call but
              I am confused by what you "had in your head"
              How are you relating
              [color=blue]
              > class ClientCode {
              > function doStuff() {
              > Thing::doStuff( );
              > }
              > }
              >
              > ...rather than this...
              >
              > class ClientCode {
              > function doStuff(&$thing ) {
              > $thing->doStuff();
              > }
              > }[/color]


              To Christian's reply ? I think im missing something small ?

              Thanks for the post

              - Eric

              "Marcus Baker" <marcus@lastcra ft.com> wrote in message
              news:3FDF9E73.9 070309@lastcraf t.com...[color=blue]
              > Hi...
              >
              > Christian Fersch wrote:[color=green][color=darkred]
              > >>The :: type of call is called a "static" invocation or "static
              > >>dispatch". When we do Thing::doStuff( ) we know exactly what piece of
              > >>code will execute. It will never change.[/color][/color]
              >
              > Doh! Confused myself :P.
              >[color=green]
              > >
              > >
              > > not true :P
              > > look at this:[/color]
              >
              > I had in my head a different scenario. One where a method does this...
              >
              > class ClientCode {
              > function doStuff() {
              > Thing::doStuff( );
              > }
              > }
              >
              > ...rather than this...
              >
              > class ClientCode {
              > function doStuff(&$thing ) {
              > $thing->doStuff();
              > }
              > }
              >
              > Must remember not to post late at night :(.
              >
              > yours, Marcus
              > --
              > Marcus Baker, marcus@lastcraf t.com, nokia@appo.demo n.co.uk
              >[/color]


              Comment

              • Marcus Baker

                #8
                Re: Php two colons &quot; :: &quot; syntax or naming convention ?

                Hi...

                Charon wrote:[color=blue]
                > Quite informative and what I was looking for. Including the other two posts.
                > But would you clarify the mistake that was made ?
                > I understood the example by Christian as a way to break the method call but
                > I am confused by what you "had in your head"
                > How are you relating
                >
                >[color=green]
                >>class ClientCode {
                >> function doStuff() {
                >> Thing::doStuff( );
                >> }
                >>}
                >>
                >>...rather than this...
                >>
                >>class ClientCode {
                >> function doStuff(&$thing ) {
                >> $thing->doStuff();
                >> }
                >>}[/color][/color]

                The first example has the class hard coded. This prevents the caller
                from intercepting the behaviour. The second class takes in a polymorph.
                Although we probably wrote the code with a Thing in mind, people are
                free to change the behaviour and use the code in new ways.

                A crude example...

                class Dollars {
                function asText($amount) {
                return (string)$amount . '\$';
                }
                }

                class LineItem {
                function LineItem() {
                }
                function asText($descrip tion, $cost) {
                return $description . ' ' . Dollars::asText ($cost);
                }
                }

                "Better" is...

                class LineItem {
                $this->_currency;

                function LineItem($curre ncy) {
                $this->_currency = $currency;
                }
                function asText($descrip tion, $cost) {
                return $description . ' ' . $this->_currency->asText($cost );
                }
                }

                Is that what you were asking?

                yours, Marcus
                --
                Marcus Baker, marcus@lastcraf t.com, nokia@appo.demo n.co.uk

                Comment

                • Charon

                  #9
                  Re: Php two colons &quot; :: &quot; syntax or naming convention ?

                  That clears it up, Thank you.

                  - Eric

                  "Marcus Baker" <marcus@lastcra ft.com> wrote in message
                  news:3FE079E5.3 040304@lastcraf t.com...[color=blue]
                  > Hi...
                  >
                  > Charon wrote:[color=green]
                  > > Quite informative and what I was looking for. Including the other two[/color][/color]
                  posts.[color=blue][color=green]
                  > > But would you clarify the mistake that was made ?
                  > > I understood the example by Christian as a way to break the method call[/color][/color]
                  but[color=blue][color=green]
                  > > I am confused by what you "had in your head"
                  > > How are you relating
                  > >
                  > >[color=darkred]
                  > >>class ClientCode {
                  > >> function doStuff() {
                  > >> Thing::doStuff( );
                  > >> }
                  > >>}
                  > >>
                  > >>...rather than this...
                  > >>
                  > >>class ClientCode {
                  > >> function doStuff(&$thing ) {
                  > >> $thing->doStuff();
                  > >> }
                  > >>}[/color][/color]
                  >
                  > The first example has the class hard coded. This prevents the caller
                  > from intercepting the behaviour. The second class takes in a polymorph.
                  > Although we probably wrote the code with a Thing in mind, people are
                  > free to change the behaviour and use the code in new ways.
                  >
                  > A crude example...
                  >
                  > class Dollars {
                  > function asText($amount) {
                  > return (string)$amount . '\$';
                  > }
                  > }
                  >
                  > class LineItem {
                  > function LineItem() {
                  > }
                  > function asText($descrip tion, $cost) {
                  > return $description . ' ' . Dollars::asText ($cost);
                  > }
                  > }
                  >
                  > "Better" is...
                  >
                  > class LineItem {
                  > $this->_currency;
                  >
                  > function LineItem($curre ncy) {
                  > $this->_currency = $currency;
                  > }
                  > function asText($descrip tion, $cost) {
                  > return $description . ' ' . $this->_currency->asText($cost );
                  > }
                  > }
                  >
                  > Is that what you were asking?
                  >
                  > yours, Marcus
                  > --
                  > Marcus Baker, marcus@lastcraf t.com, nokia@appo.demo n.co.uk
                  >[/color]


                  Comment

                  Working...