By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Charon <er********@edu.sait.ab.ca> wrote:
$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


It's in the reference under the "Classes and Objects" section:
http://www.php.net/manual/en/keyword...ekudotayim.php

It's a static accessor into objects.

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
Charon wrote:
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.


http://dk.php.net/manual/en/keyword....ekudotayim.php

:)

Regards,
Johan

Jul 17 '05 #3

P: n/a
Hi...

Hi...Charon wrote:
I have looked all over the place and have been unable to figure out
exactly what the :: means or does ?


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, ma****@lastcraft.com, no***@appo.demon.co.uk

Jul 17 '05 #4

P: n/a
> 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.


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/
Jul 17 '05 #5

P: n/a
Hi...

Christian Fersch wrote:
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.

Doh! Confused myself :P.


not true :P
look at this:


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, ma****@lastcraft.com, no***@appo.demon.co.uk

Jul 17 '05 #6

P: n/a
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
class ClientCode {
function doStuff() {
Thing::doStuff();
}
}

...rather than this...

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

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

Thanks for the post

- Eric

"Marcus Baker" <ma****@lastcraft.com> wrote in message
news:3F**************@lastcraft.com... Hi...

Christian Fersch wrote:
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.


Doh! Confused myself :P.


not true :P
look at this:


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, ma****@lastcraft.com, no***@appo.demon.co.uk

Jul 17 '05 #7

P: n/a
Hi...

Charon wrote:
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

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

...rather than this...

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


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($description, $cost) {
return $description . ' ' . Dollars::asText($cost);
}
}

"Better" is...

class LineItem {
$this->_currency;

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

Is that what you were asking?

yours, Marcus
--
Marcus Baker, ma****@lastcraft.com, no***@appo.demon.co.uk

Jul 17 '05 #8

P: n/a
That clears it up, Thank you.

- Eric

"Marcus Baker" <ma****@lastcraft.com> wrote in message
news:3F**************@lastcraft.com...
Hi...

Charon wrote:
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

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

...rather than this...

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


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($description, $cost) {
return $description . ' ' . Dollars::asText($cost);
}
}

"Better" is...

class LineItem {
$this->_currency;

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

Is that what you were asking?

yours, Marcus
--
Marcus Baker, ma****@lastcraft.com, no***@appo.demon.co.uk

Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.