Different ways to access methods

3.1k Views Asked by At

I have seen that there are two different ways to access methods within a class. Are there any differences in behaviour, or are they purely alternative syntaxes for the same action?

$a = new A();
$a->foo();

A::foo();
5

There are 5 best solutions below

4
On BEST ANSWER

You can't just use one or the other. :: is for static methods and variables, whereas -> is for instance methods and variables. This is "inspired" from C++ syntax.

class A {
    public function __construct() {}
    public function foo() {}
}
$a = new A();
$a->foo();
// Or use the shorter "new A()->foo()";
//   It won't return typeof(A), it will return what foo() returns.
// The object will still be created, but the GC should delete the object

or

class A {
    public static function foo() {}
}
A::foo();

According to DCoder, :: can be used for calling parent methods, but I don't know this for sure.

class B {
    public function __construct() {}
    public function foo() {}
}
class A extends B {
    public function __construct() {
        // Code
        parent::__construct()
    }
    public function foo() {
        // Code
        parent::foo()
    }
}
0
On

You will access foo() method with class::foo() if foo() is a static method. Otherwise, you will use object-­>foo() for instance methods.

0
On

Using the :: operator accesses the class statically. In this case, there isn't an instance of the class as an object. It has it's uses in different OOP design patterns such as Singleton and Factory methods. The scope of accessing this is almost global and can get you in trouble if you don't know what you are doing.

Usage of the -> operator means you are accessing a method or variable of an instantiated class. In this case, the scope is within the specific object you instantiated and removes that global state.

0
On

-> will access the method of a particular object that was instantiated from class A.

:: is the scope resolution operator which refers to static members of a specified class( in your case class A ).

If you haven't noticed with -> you access method of an object $a that WAS created from class A( $a = new A(); ), but with :: you access static methods( static methods don't need objects to be accessed ).

For example.

Assume you have a class named Math which has a static method Add():

<?php
class Math{
// Note the 'static' keyword
public static function Add($arg1, $arg2){
return $arg1+$arg2;
}
}
?>

In this case you could use the Add() method straight away without the need to create any object of class Math like so:

<?php
print Math::Add(2, 3);
?>

You use the :: symbol to access the static method of Math class. It wouldn't make sense to use -> symbol because it is used to access methods of objects that were created from classes.

So:

    <?php
    class Math{
    // Add() NO LONGER static
    public function Add($arg1, $arg2){
    return $arg1+$arg2;
    }
    }
?>

And you would have to do this instead:

<?php
$object = new Math();
$result = $object->Add(2, 3);
print $result;
?>
0
On

When you do

$a = new A();

You have an instance of an object class named A.

This implies that you have an especific object (with its own personality, concrete values in his attributes).

So, you can access properties and call methods for this object with expressions like:

$a->foo();

On the other hand,

A::foo();

is the proper way to access properties and methods that reside on the class itself (in the definition of the class). These are called static.

The main difference is that the static methods can be accessed before creating any object of that class, i.e. no need to use the new operator. They are used to cross-object attributes and object-wide methods.

The instances of the object (created with the new operator) are real copies of the class, that can have different values to their attributes. Two objects of the same class can have different values inside them for the same properties. In the static portion of the class, all objects will have the same values and behaviour.

Hope that helped!