473,390 Members | 1,418 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,390 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 7908
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.