473,594 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7917
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****@lastcraf t.com, no***@appo.demo n.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****@lastcraf t.com, no***@appo.demo n.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****@lastcra ft.com> wrote in message
news:3F******** ******@lastcraf t.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****@lastcraf t.com, no***@appo.demo n.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($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, ma****@lastcraf t.com, no***@appo.demo n.co.uk

Jul 17 '05 #8
That clears it up, Thank you.

- Eric

"Marcus Baker" <ma****@lastcra ft.com> wrote in message
news:3F******** ******@lastcraf t.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($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, ma****@lastcraf t.com, no***@appo.demo n.co.uk

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
5242
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
4
7140
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 convensions. The thing is that Ive never really get the full reference to check against. Ive seen a couple of articles, but there always seems to be a bit missing. I also always seem to run into conflicting convensions both in code samples themselves...
48
4723
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 can pick up any mistakes/"don't do"s from this code: #include <iostream> #include <cstdlib>
60
5015
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. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
114
7810
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 any comments. --
35
12165
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 recommended practise? Thanks.
2
1250
by: Fir5tSight | last post by:
Hi, I have a stored procedure that looks like the follows: ------------------------------------------------------------------------------------- SELECT ClientName AS 'Client Name', Location, ReportInstanceID FROM
2
1786
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 ..php file is try to create a class of type <filenameminus the .php, so eg. for testmodule.php it tries to create a class "testmodule" and puts it into an array within the module manager called $_modules Module Manager has a dispatch_message...
49
2870
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
7946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8253
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.