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 Object-Oriented PHP Basics (Retired) Properties and Methods Properties

Presley Cobb
Presley Cobb
9,214 Points

I dont understand what "->" is.

In the video he calls this an object operator. I cant seem to find the documentation on this online. At least not any that makes sense to me. When I think or operators I think or + - * and all the other mathematical operators but this seems do go by a different set of rules can anyone explain this or at least point me to the documentation on php.net or anywhere else.

5 Answers

-> is how you set a class variable from outside the class and call the function from outside the class also. Then you use it inside the class to set variables as well as part of the constructor function.

Consider this code:

<?php

class Fish {
 Public $common_name;
Public $flavor;
Public $record_weight;

   function __construct($name,$flavor,$record){
    $this-> common_name=$name;
    $this-> flavor=$flavor;
    $this-> record_weight=$record;
  }

    Public function getInfo(){
      return "A" .  $this->common_name .  "is an" . $this->flavor . "flavored fish.The world record weight is" . $this->record_weight; //used record_weight and added concatenation
  }
}

$bass = new Fish('Largemouth Bass','Excellent','22 pounds 5 ounces');

?>

A class fish is defined and includes variable definitions, a constructor function to accept input and assign it to class variables.

$bass creates a new instance of the Fish class. We could also create a new $tuna, new $sword_fish, and new $sturgeon at the same if we wanted. Each one would allow the class to function.

$this is a special word that has several different meanings. In this context it is assigning the variable for each instance. So $flavor means different things for each instance of the class.

If we wanted to echo the getInfo() method for $bass, we would use:

<?php
echo $bass->getInfo();

Take the course again if you need to. I have watched several of the videos 4 or 5 times and am not 100% sure I can implement it on my own. I am currently working on a project that uses OOP for database work. it is really stretching my knowledge and building my understanding.

-> tells PHP To do something with the class that you call. You can set variables or call functions in the class by using it.

It took me multiple time watching the videos and trying to write my own code to start figuring this out. I have found OOP to be one of the harder concepts to get my hands around, so stick with it.

Presley Cobb
Presley Cobb
9,214 Points

Well I figured that it does something with the class like the other operators do. But that kind of implies there are other operators that do other things. Is there a list I can read through that you know of? Setting a variable and calling a function seem like very different things. Outside of OOP you would use "=" to set a variable/property and "()" to call a function/method. Are you saying that "->" does both?

akin akcn
akin akcn
18,557 Points

Hey Ted

Thanks for great example , You helped me to understand OOP in php with this example.

Thank you.