What's the Difference Between :: (Double Colon) and -≫ (Arrow) in PHP

Difference between double colon and arrow operators in PHP?

:: is for static elements while -> is for instance elements.

For example:

class Example {
public static function hello(){
echo 'hello';
}
public function world(){
echo 'world';
}
}

// Static method, can be called from the class name
Example::hello();

// Instance method, can only be called from an instance of the class
$obj = new Example();
$obj->world();

More about the static concept

Difference between double colon and arrow in perl

Foo->bar() is a method call.

  • It will use inheritance if necessary.
  • It will pass the invocant (the left side of ->) as the first argument. As such, bar should be written as follows:

    # Class method (Foo->bar)
    sub bar {
    my ($class, ...) = @_;
    }

    or

    # Object method (my $foo = Foo->new; $foo->bar)
    sub bar {
    my ($self, ...) = @_;
    }

Foo::bar() is a sub call.

  • It won't use inheritance.

What is the difference between using double arrow and single colon when define an array in PHP

it's completely different. when you use

 $header = array(
'Content-type: application/json',
'x-app-key: 123'
);

you're just defining an array of strings. the array would be

array(2) { 
[0]=> string(30) "Content-type: application/json"
[1]=> string(14) "x-app-key: 123"
}

but if you use

$header = array(
'Content-type' => 'application/json',
'x-app-key' => '123'
);

you will create an associative array, like so

array(2) { 
["Content-type"]=> string(16) "application/json"
["x-app-key"]=> string(3) "123"
}

please refer to this documentation for a complete explanation

hope it helps!

Difference between :: and -> in PHP?

:: is for calling static methods, -> is for instance methods

PHP: Difference between -> and ::

$name = $foo->getName();

This will invoke a member or static function of the object $foo, while

$name = $foo::getName();

will invoke a static function of the class of $foo. The 'profit', if you wanna call it that, of using :: is being able to access static members of a class without the need for an object instance of such class. That is,

$name = ClassOfFoo::getName();

What is the difference between => and -> in php?

Below link will provide you a full list of symbols and their usage.

Reference - What does this symbol mean in PHP?

Since you have asked, In short

=> is called T_DOUBLE_ARROW and is the separator for associative arrays, the '=>' created key/value pairs.

-> is called "object operator" or T_OBJECT_OPERATOR and it's used when you want to call a method on an instance or access an instance property.

Why does the PHP double arrow operator '=>' stop the execution of the code

You haven't quoted either of those values, so PHP is treating them as undefined constants.

Try

$data = array('grant_type' => $authorization_code);

or whatever it really should be instead.

php > $data = array(foo => bar);
PHP Notice: Use of undefined constant foo - assumed 'foo' in php shell code on line 1
PHP Notice: Use of undefined constant bar - assumed 'bar' in php shell code on line 1
php > define('foo', 'hello');
php > define('bar', 'world');
php > $data = array(foo => bar);
php > var_dump($data);
array(1) {
["hello"]=>
string(5) "world"
}

As far as execution stopping goes, => shouldn't be a PHP block sentinel value. only ?> would "shut off" php.

PHP: How to create function which will be accessible with :: (double colon, scope resolution) in another classes

The double colon allows access to static function and constants in a class.

Change your class method to:

static function runTests() {
...

and then call it like this

DoSomethin::runTests();


Related Topics



Leave a reply



Submit