What do two colons mean in PHP? Colon: where, when and why you need to put a colon How you write.

If you have analyzed the codes of any programs written in PHP, then you have probably encountered such special things in the code. characters:

-> (Dash and greater than sign)

:: (Double colon)

For example, constructions with these expressions might look like:

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

To understand why these structures are needed, let's look at the simplest example. Let's say 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 words “Hello world” appear on the screen. But most likely, if you tried to solve the problem this way, you got an error on your screen:

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

Everything is correct, because function is in a class, then you need to tell the computer that it needs to be looked for in the Hello class. If you don't do this, then nothing will work.

How can such information be communicated to a computer?

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

Option 1. Through creating an instance of a 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 a class instance and a function.

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

Option 2. Using the double colon construction::

This option is even much simpler. You don't even need to create any instances here. We're just writing.

Hello::hello_world();

You can check if the function will be executed successfully.

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

Using the same constructs, we can access variables that are located in classes. The only difference is that you don't need to put parentheses at the end. For example:

Hello::world;

With this construction we access the world variable, which is located in the Hello class.

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

More of my PHP tutorials for beginners.

So, the colon is a punctuation mark. Unlike a 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. A colon is placed only within a sentence, and therefore indicates that the statement will be continued after it.

Colons can be found in simple and complex sentences. In a simple sentence it divides and highlights parts of the statement; in a complex sentence it also plays a semantic role: it indicates the semantic relationships between the parts of a non-union complex sentence.

So how to use a colon correctly, 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 a generalizing word comes before 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 generalizes 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 are used in a sentence, clarifying words can be used: like, for example, like 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 a construction clarifying it was used "namely" , followed by a series of homogeneous terms, 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 in front of it, and the direct speech itself is enclosed in quotation marks and the first word of direct speech is written with a capital letter: The teacher said: “Hello, children!” Also, if direct speech breaks the author’s words in the text, then a colon is placed in front of it, for example: Saying: “Hello, children!”, the teacher entered the classroom. After direct speech, you need to put a comma.

Colon in a complex sentence

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

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

  • Second part of the BSP reveals the reason what was discussed in the first (you can insert conjunctions between parts of the sentence: since, because), For example: Katya writes very competently: she reads a lot, learns poetry by heart, trains her memory; It will rain soon: swallows fly low over the water. In these BSPs, the second part justifies and indicates the reason for what was said in the first. You can insert conjunctions between parts: since, because. It's going to rain soon (because): swallows fly low over the water. That's why a colon is used in such sentences.

  • Second part of the BSP complements, reveals what was said in the first (such sentences are synonymous with complex sentences with an explanatory clause, therefore, a conjunction can be inserted between the 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 when it rains. The BSP data in the second part reveals the meaning of what is discussed in the first. If the parts of such a sentence are connected by the conjunction What, then you get a complex sentence with an explanatory clause: People say (What,): swallows fly low over the water towards the rain. Sentences of this type are preceded by a colon.

  • 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 was breaking through the clouds, illuminating the clearing with bright light; He turned around: the echo of hasty steps was heard very close. These sentences are also synonymous with complex subordinate clauses with explanatory clauses; in the first part, verbs like: and I saw and I heard, You can insert a conjunction 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 sometimes it is easy to confuse this punctuation mark with another - 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, decided there was treasure here, so I started poking around(Tendryakov), Of course hechanged(: –) hunched over, gray, with wrinklesin the corners of the mouth; I'm coming in(: ) everything is quiet; Don't say these words in front of him.(: ) may be offended; Everyone was interested in only one question(: ) how to get out of this situation as quickly as possible; It meant one thing(: ) need to break up.

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

You've probably noticed how often I use these two pseudo-elements in my lessons. 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 once and for all clarifying all the confusion regarding the syntax of pseudo-elements.

Pseudo-what?

Pseudo-elements are so called because they are not real HTML elements. For example, HTML doesn't have before and after tags, but in CSS you can style the :before and :after elements.

One or two colons?

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

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

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

Let's summarize

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

Important!

Please remember that all this talk about 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.

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 aliased class name to its fully qualified form, and

    To call the class method __callStatic.

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

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

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

The "::class" syntax appears to work for resolving interface names as well as class names, although this does not seem to be documented by the sources linked above.

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

A few examples:

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

To call "__callStatic"

Two colons can be used to "call" a static method name that the class has not declared. This calls the class method "__callStatic" if it has declared one. The __callStatic method can then "dynamically" choose how to handle the call. In PHP this is called "overloading" using 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.

Latest materials in the section:

Elective culture media
Elective culture media

Nutrient media in microbiology are substrates on which microorganisms and tissue cultures are grown. They are used for diagnostic...

Rivalry of European powers for colonies, the final division of the world at the turn of the 19th - 20th centuries
Rivalry of European powers for colonies, the final division of the world at the turn of the 19th - 20th centuries

World history contains a huge number of events, names, dates, which are placed in several dozen or even hundreds of different textbooks....

It should be noted that over the years of palace coups, Russia has weakened in almost all areas
It should be noted that over the years of palace coups, Russia has weakened in almost all areas

The last palace coup in the history of Russia Vasina Anna Yuryevna Lesson “The last palace coup in the history of Russia” LESSON PLAN Topic...