tags:

views:

703

answers:

8

In my work I have come across syntax like global:: (C#) or G:Loading($value) in Php, what do they mean?

+4  A: 

In PHP :: is used to access static members or methods, see http://www.php.net/manual/en/language.oop5.static.php

Bob
+9  A: 

In C# :: is the namespace alias qualifier operator, and global indicates the global namespace. It is necessary if you have overlapping classes and namespaces.

For example, if you have class Foo.Bar and Baz.Foo.Bar then the reference Foo.Bar may be an ambiguous reference as Foo.Bar exists in both the global namespace and the Baz namespace. However, by saying global::Foo.Bar you explicitly scope the reference to the global namespace and thus it is no longer ambiguous.

More info here.

Greg Beech
+2  A: 

If C#, :: is called the Namespace Alias Qualifier.

It qualifies which namespace you are in exactly should there be ambiguities. For example:

using MyNameSpace;

...

global::System.Data

differentiates the System.Data namespace from MyNameSpace.System.Data.

Have a look at C# reference on the :: operator:

http://msdn.microsoft.com/en-us/library/htccxtad.aspx

and more helpfully, the MSDN article demonstrating its use

http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

biozinc
+1  A: 

In C# the global:: operator is used to ensure that the following name is interpreted as a global name expression vs. a local one. The main use for this is when developers have a nested namespace where the name is the same as a global namespace. Without the global:: operator there is no way to access the global version.

namespace Foo { 
  namespace System {
    class DateTime {} 
    class Example {
      object Example() { 
        return System.DateTime.Now; // Returns Foo.System.DateTime.Now
        return global::System.DateTime.Now; // Returns mscorlib,System.DateTime.Now
      }
    }
  }
}
JaredPar
+1  A: 

In PHP its called the Scope Resolution Operator (Paamayim Nekudotayim) and allows access to members or constants within a class

Ólafur Waage
Btw fun trivia about the hebrew name here: http://en.wikipedia.org/wiki/Paamayim_Nekudotayim
Ólafur Waage
A: 

In C#, the :: operator is the namespace alias qualifier. It is used to resolve identifiers to the appropriate namespace, especially in situations where ambiguities exist.

So suppose that you create a namespace alias via

using IOAlias = System.IO;

and wish to declare an instance of the StreamReader class. You could do this via

IOAlias::StreamReader sr = new StreamReader(path);

Further, you can resolve to the global namespace via

global::System.IO.StreamReader sr = new StreamReader(path);
Jason
+2  A: 

in Class based object oriented programming languages (like C++, Java, PHP, ...) there are properties (also called fields) which are the data and are simply variables or constants, and methods which are the functionality of the class and are actually functions (routins). each class can be instanciated as objects. each object has all data and functionality of the base class at the beginning, but the data can be changed and be different from one object, to another. there are 2 kinds of properties and 2 kinds of methods. some properties are shared between all objects of a class. this means that if you change this data of the class, the same data of all the instanciated objects will be changed to the neew value. this kind of properties are called Class properties, or Static properties (because when you are defining these variables, in most languages you will use the static keyword). some other properties are specific for each object. changing the data of on object, does not affect the data of other objects. these are called object properties or instance properties. some methods are called from an object, there are called object methods. these are object methods and to use them, you need to first instanciate a class and use the object. some other methods are called by using the class itself. these are class methods (or static methods) if you want one of these functions, there is no need for an object. to use object properties/methods, in PHP (like C++) you should use the -> operator:

object->method();
object->property;

to use class properties/methods in PHP (like C++) you should use the :: operator:

class::method();
class::property;

in languages like Jave, the operator . is used for all of properties/methods:

object.method();
class.property;

suppose you have a Circle class. this class defines some data like radius, and center cordinates, and a constant for PI value. also has a method to calculate the area of the circle.

here is some PHP code to make it clear:

<?php
class Circle {
    public $radius;
    public $centerX;
    public $centerY;
    public static $PI = 3.1415;
    public function __construct($r,$x,$y) {
        $this->radius = $r;
        $this->centerX = $x;
        $this->centerY = $y;
    }
    public function getArea() {
        return self::$PI * $this->radius * $this->radius;
    }
}
$circle1 = new Circle(10,0,0);
echo $circle1->getArea() . "<br>\n";
$circle2 = new Circle(15,10,12);
echo $circle2->getArea() . "<br>\n";
Circle::$PI = 3.14;
echo $circle1->getArea() . "<br>\n";
echo $circle2->getArea() . "<br>\n";
?>

in this example we define a circle class which has object properties (radius, centerX, CenterY), object methods (getArea()), and a Class property (PI). by changing the PI static property, the area of both objects will be affected.

farzad
A: 

Consider the following example:

namespace A {

    public class MyClass { }

}

namespace namespaceB
{
    public class A
    {
        public class MyClass { }
    }

    public class OtherClass
    {
        public A.MyClass MyAClass;
    }
}

 Explanation
  In the example above, the compiler would always resolve the
  MyAClass to the type namespaceB.A.MyClass. 
 If you wanted it to be A.MyClass, there   was no way to do it until C# 2.0. 
 In C# 2.0, you would use: public class MyOtherClass
 { public global::A.MyClass yAClass; }
To indicate that you shouldn't use the local namespace scope,
but rather, the root namespace.
Prajeesh Karayil