473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
Jul 17 '05 #1
8 7905
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
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
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
> 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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
48
by: mahurshi | last post by:
I am new to c++ classes. I defined this "cDie" class that would return a value between 1 and 6 (inclusive) It runs fine and gives no warnings during compilation. I was wondering if you guys...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
35
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or...
2
by: Fir5tSight | last post by:
Hi, I have a stored procedure that looks like the follows: ------------------------------------------------------------------------------------- SELECT ClientName AS 'Client Name', Location,...
2
by: Tyno Gendo | last post by:
I'm writing a test "modular site". So far I have created an App class, a Module Manager class and a couple of test modules. The Manager looks in a directory called 'modules' and then for every...
49
by: aarklon | last post by:
Hi all, See:- http://www.cs.princeton.edu/introcs/faq/c2java.html for C vs Java in number crunching http://husnusensoy.blogspot.com/2006/06/c-vs-java-in-number-crunching.html
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.