Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Lucas Santos
Lucas Santos
19,315 Points

What's the difference between -> and => in php

I read that -> sets keys to values but I also know that => also sets keys to values in an array. I see the -> in loops like this foreach(loops as loop->black); and many other places.

4 Answers

Kazimierz Matan
Kazimierz Matan
13,257 Points

Generally:

=> is considered an array operator and is meant to be used with arrays

-> is an object operator and should be used in OOP (Object Oriented Programming) to call methods on objects.

Lucas Santos
Lucas Santos
19,315 Points

Can you give me a quick simple example of how I use -> to call methods on objects.

Kazimierz Matan
Kazimierz Matan
13,257 Points
<?php

class Products
{
    public function getPrice()
    {
        // some code
    }
}

$product = new Products;

$product->getPrice();

Last line of above code calls method named getPrice() on object named $product.

Lucas Santos
Lucas Santos
19,315 Points

I see ok so in other words the code within the function (method) would now be assigned to getPrice(); rite

Kazimierz Matan
Kazimierz Matan
13,257 Points

Code after public function getPrice(), within brackets {}, is a body of getPrice method (function). It is executed every time you call this function, like $product->getPrice();.

Lucas Santos
Lucas Santos
19,315 Points

Oh I see ok got it thanks!