If you have analyzed the codes of any programs written in the php language, then, for sure, you have come across such special codes in the code. symbols:

-> (Dash and greater than sign)

:: (double colon)

For example, constructs with these expressions might look like:

Hello::hello_world(); q -> hello_world();

To understand what these constructs are for, let's look at the simplest example. Suppose we have a simple "Hello world!" program.

Let's create a simple php file and write the following code in it:

Class Hello ( function hello_world () ( echo "Hello world"; ) )

Now think about how you can execute the hello_world() function.

If the hello_world() function were not in the Hello class, then we would call it very simply:

Hello_world();

As a result, we would have the inscription "Hello world" on the screen. But, most likely, if you tried to solve the problem in this way, you got an error on the screen:

Fatal error: Call to undefined function hello_world() in …/test.php on line…

Everything is correct, because function is in the class, then you need to tell the computer that it should be looked for in the Hello class. If this is not done, then nothing will work.

How can such information be communicated to a computer?

There are several options here. Let's look at each separately.

1 option. By creating an instance of the class.

$q = new Hello;

With this line, we create a so-called instance of the Hello class. After this operation, we can freely access the function we need through this instance, which in this case is stored in the $q variable.

$q -> hello_world();

The "->" construction becomes the connecting link between the class instance and the function.

Now everything works. Hooray! You should see "Hello world" on the screen.

Option 2. Using the double colon construct:

This option is much easier. You don't even need to create any instances here. We just write.

Hello::hello_world();

You can check if the function will complete successfully.

I hope that after reading this note, you now understand that "->" and "::" are used to access the desired function in the created class.

With the help of the same constructions, we can access variables that are in classes. The only difference is that you don't need to put brackets at the end. For example:

hello::world;

With this construct, we refer to the world variable, which is located in the Hello class.

Especially often, such constructions can be found when parsing the code of various site management systems. Look at the source code of such systems as Joomla, WordPress, etc. Now you know what all this is for.

More of my PHP tutorials for beginners.

In addition to the answers regarding PHP's use of two colons as a "scope resolution operator":

In addition, a double colon is used:

    To convert an unqualified, qualified, or alias class name to its fully qualified form, and

    To call a class method __callStatic.

To convert a class name to its fully qualified form by adding "::class"

Two colons followed by the "class" keyword after the class name provide full name of this class as a string. That is, "ClassName::class" is converted to the fully qualified name "ClassName". See (A) Manual: Classes and Objects: Fundamentals, (B) Manual: Classes and Objects: Class Constants, and (C) Manual: Language Reference: Constants

The "::class" syntax is useful in a namespace for deriving the fully qualified name of a class from its unqualified or qualified form, or from an alias of its name.

The "::class" syntax seems to work for resolving interface names as well as class names, although this doesn't seem to be documented by the sources cited above.

Within a class, the syntax also works with "self::class" as mentioned in the "::class" RFC above.

A few examples:

fullName(), PHP_EOL; // outputs: MyNamespace\TheirClass

To call "__callStatic"

Two colons can be used to "invoke" a static method name that the class has not declared. This calls the "__callStatic" class method if it has declared one. The __callStatic method can then "dynamically" choose how to handle the call. In PHP this is called "overloading" with the "magic method" __callStatic .

handleOrder($arguments); ) return "I"m sorry, we can"t help you with " . lcfirst($item) . "."; ) ) namespace OurCompany\Specialists; class Car ( public function handleOrder($arguments) ( return "May I help you with a $arguments car?"; ) ) class Truck ( public function handleOrder($arguments) ( return "May I help you with a $arguments truck? "; ) ) use OurCompany\Orders\Intake; echo Intake::orderCar("red"), PHP_EOL; // outputs: May I help you with a red car? echo Intake::orderTruck("pickup"), PHP_EOL; // outputs: May I help you with a pickup truck? echo Intake::orderShoes("suede"), PHP_EOL; // outputs: I "m sorry, we can" t help you with shoes.

You may have noticed how often I use these two pseudo-elements in my tutorials. Why do I like them? Because they are so convenient to use that you don't even need to add additional HTML elements. However, this article is not about the benefits of pseudo-elements.

This article is dedicated to clarifying once and for all all the misunderstandings regarding the syntax of pseudo-elements.

Pseudo what?

Pseudo-elements are so called because they are not real HTML elements. For example, in HTML there are no before and after tags, but in CSS you can apply styles to the :before and :after elements.

One or two colons?

There is no difference between :before and::before or between :after and::after. The two-colon syntax is specific to CSS3.

http://www.w3.org/TR/css3-selectors/#pseudo-elements has this paragraph:

This sign (::) is introduced to distinguish pseudo-classes from pseudo-elements. For compatibility with pre-existing style sheets, pseudo-elements in CSS 1 and 2 can also be specified with a single colon (in particular, :first-line, :first-letter, :before and :after).

Summarizing

IE 7 and earlier do not support the listed pseudo-elements. But IE 8 does, and that's a good (and only) reason to use the single-colon syntax.

Important!

Please keep in mind that all this talk of one or two colons is only about pseudo-elements, not pseudo-classes.

How do you write?

Write in the comments about which syntax you use, and for what reasons.

So, the colon is a punctuation separator. Unlike the period, exclamation and question marks, and ellipsis, it does not have a separating function, that is, it does not separate sentences in the text from each other. The colon is placed only inside the sentence, and therefore indicates that after it the statement will be continued.

The colon can be found in simple and complex sentences. In a simple sentence, it separates and highlights parts of the statement; in a complex one, it also plays a semantic role: it indicates the semantic relationships between the parts of an unassociated complex sentence.

So what is the correct way to use a colon without making mistakes and not confusing it with others? Let's look at the main cases.

Colon in a simple sentence

Rule 1. Colon in a sentence with homogeneous members

If the generalizing word is in front of homogeneous members, then a colon is needed after it, for example: snow lay everywhere: on the roofs of houses, on fences, on lawns, on cars.

In this sentence, the word everywhere summarizes a number of similar circumstances. The generalizing word is located before homogeneous members, and therefore there is a colon after it.

Rule 2. Colon in sentences with qualifying words

When generalizing words in a sentence, qualifying words can be used: as for example, for example, as that, namely, preceded by a comma and followed by a colon. For example: snow lay everywhere, namely: on the roofs of houses, on fences, on lawns, on cars.

In this sentence, together with the generalizing word everywhere used a refinement of its construction "namely" , followed by a series of homogeneous members, so it is followed by a colon.

Rule 3. Colon in constructions with direct speech

If direct speech follows the words of the author, then a colon is placed before it, and the direct speech itself is enclosed in quotation marks and the first word of direct speech is capitalized: The teacher said: "Hello, children!". Also, if direct speech breaks the words of the author in the text, then a colon is placed in front of it, for example: Saying: "Hello, children!", The teacher entered the classroom. A comma must be placed after direct speech.

Colon in compound sentence

Rule 4. A colon can be placed between the parts of a non-union complex sentence (BSP) in the following cases.

  • The second part of the BSP explains what was said in the first (between the parts of the sentence you can insert " namely"), for example: There is such a sign among the people: swallows fly low to the rain; Katya writes very competently: she never makes mistakes in words, she correctly places punctuation marks. In these non-union complex sentences, in the second part, an explanation is given of what is being discussed in the first. Between parts of sentences you can insert " namely": There is a saying among the people (namely): swallows fly low over the water to rain. Therefore, in the above sentences, a colon is used.

  • The second part of the BSP reveals the reason what was said in the first (conjunctions can be inserted between the parts of the sentence: because, because), for example: Katya writes very competently: she reads a lot, learns poetry by heart, trains her memory; It will soon rain: swallows fly low over the water. In these BSPs, the second part substantiates, indicates the reason for what was said in the first. Unions can be inserted between parts: because, because. It's gonna rain soon (because): swallows fly low over the water. Therefore, in such sentences, a colon is used.

  • The second part of the BSP complements, reveals what was said in the first one (such sentences are synonymous with complex sentences with an explanatory clause, so a union can be inserted between parts of the BSP what). For example: Katya knows: reading books is not only interesting, but also very useful for studying; People say: swallows fly low over the water to rain. In the BSP data in the second part, the meaning of what is discussed in the first part is revealed. If the parts of such a sentence are connected by a union what, then you get a complex sentence with an explanatory clause: The people say (what,): swallows fly low over the water to rain. In sentences of this type, a colon is used.

  • In the first part of the sentence, the verbs are omitted: and saw and heard and felt, for example: I raised my head: a ray of sun broke through the clouds, illuminating the clearing with a bright light; He turned around: the echo of hurried footsteps was heard very close. These sentences are also synonymous with a complex subordinate clause with an explanatory clause; verbs of the type are omitted in the first part: and saw and heard union can be inserted between parts what. I raised my head (and saw that): a ray of sun broke through the clouds, illuminating the clearing with bright light.


Colon vs dash

The main difficulty in using a colon is that it is sometimes easy to confuse this punctuation mark with another one - a dash. Recently, these signs are often used in parallel, that is, in the same cases, both a dash and a colon can be placed. For example: And the judges decided: if it rains, the competition will be canceled; I noticed the first stone, I decided - there is a treasure here, I started poking around(Tendryakov), Of course hechanged(: –) hunched, gray, wrinkledin the corners of the mouth; I'm coming in(: ) everything is quiet; Do not say these words in front of him(: ) may be offended; Everyone was interested in only one question(: ) how to get out of the situation as soon as possible; It meant one thing(: ) need to part.

Such options are considered equal, you can use both a dash and a colon without fear of making a mistake. However, it must be remembered that the dash, in addition to syntactic functions, also performs emotional and expressive ones, that is, indicates the emotionality of the statement, emphasizes its expressiveness. Therefore, if the sentence does not carry such meanings, is neutral, you need to avoid the use of a dash and put a colon, following the rules we have considered.