tags:

views:

157

answers:

4

Noob to PHP here, I've come across 2 distinct function calls which I'd like to clarify...

Whats the difference between

$response->setParameter('foo', 'bar');

and

sfConfig::set('foo', 'bar');

I'm assuming -> is used for functions for variables, and :: is used for functions for classes. Correct?

Different question, is the => assignment operator only used to assign data within an array? Is this in contrast to the = assignment operator which is used to instantiate or modify a variable?

Thanks for any and all help.

+10  A: 

When the left part is an object instance, you use ->. Otherwise, you use ::.

This means that -> is mostly used to access instance members (though it can also be used to access static members, such usage is discouraged), while :: is usually used to access static members (though in a few special cases, it's used to access instance members).

In general, :: is used for scope resolution, and it may have either a class name, parent, self, or (in PHP 5.3) static to its left. parent refers to the scope of the superclass of the class where it's used; self refers to the scope of the class where it's used; static refers to the "called scope" (see late static bindings).

Example:

class A extends B {
    public static $prop_static;
    public $prop_instance;

    public function func_instance() {
        /* this is one exception where :: is required to access instance member.
         * The super implementation of func_instance is being access here */
        parent::func_instance();
    }
    public static function func_static() {
    }
}

A::$prop_static;
A::func_static();
$a = new A;
$a->prop_instance;
$a->func_instance();
Artefacto
excellent. thank you.
Joe
" `->` is mostly used to access instance members (though it can also be used to access static members, such usage is discouraged)"I wasn't aware it could be. So if it "functions" in some way when used to access static members - what difference in behaviour would one expect if one were to use it incorrectly like this? Just out of curiosity.
lucideer
@lucideer In case of static methods, it's a question of good practice (the method belongs to the class itself), but PHP doesn't complain if call a static method with `->`. Of course, you may need to instantiate the class just to call a static method, so there's also a performance hit. With properties, however, there are more issues. A STRICT warning is raised and it [may or may not work](http://codepad.viper-7.com/d3oH0Q). Note that the reverse is also true -- you can call an instance method statically, but this is even worse (and you can't use `$this` in such method implementation).
Artefacto
+3  A: 

The => operator is used to assign key-value pairs in an associative array. For example:

$fruits = array(
  'Apple'  => 'Red',
  'Banana' => 'Yellow'
);

It's meaning is similar in the foreach statement:

foreach ($fruits as $fruit => $color)
  echo "$fruit is $color in color.";
casablanca
He, I had only read the title of the question :p +1 for completing the answer.
Artefacto
+2  A: 

:: is used in static context, ie. when some method or property is declared as static:

class Math {
    public static function sin($angle) {
        return ...;
    }
}

$result = Math::sin(123);

Also :: operator (I'm sorry but it's almost 5 o'clock and I don't remember its name :]) is used in dynamic context when you invoke a method/property of a parent class:

class Rectangle {
     protected $x, $y;

     public function __construct($x, $y) {
         $this->x = $x;
         $this->y = $y;
     }
}

class Square extends Rectangle {
    public function __construct($x) {
        parent::__construct($x, $x);
    }
}

-> is used in dynamic context, ie. when you deal with some instance of some class:

class Hello {
    public function say() {
       echo 'hello!';
    }
}

$h = new Hello();
$h->say();

By the way: I don't think that using Symfony is a good idea when you don't have any OOP experience.

Crozin
@Crozin :: is called the scope resolution operator, and if you're in the mood for a tongue twister, the paamayim nekudotayim :)
tomit
+1  A: 

The difference between static and instantiated methods and properties seem to be one of the biggest obstacles to those just starting out with OOP PHP in PHP 5.

The double colon operator (which is called the Paamayim Nekudotayim from Hebrew - trivia) is used when calling an object or property from a static context. This means an instance of the object has not been created yet.

The arrow operator, conversely, calls methods or properties that from a reference of an instance of the object.

Static methods can be especially useful in object models that are linked to a database for create and delete methods, since you can set the return value to the inserted table id and then use the constructor to instantiate the object by the row id.

DeaconDesperado
Yeah, I love it when people encounter 'Unexpected T_PAAMAYIM_NEKUDOTAYIM' for the first time.
Alex JL
Recalling googling it in utter confusion is one of my fonder memories.
DeaconDesperado