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

jamesjones21
jamesjones21
9,260 Points

Question regarding Object Oriented PHP with the syntax involved.

I'm following Aleana's videos through OOP, I understand the concepts of the keyword $this, self etc but when she refers to $this->property_name->method_name(); I get confused.

Is this saying we are passing the property to the method? My description is where she returns $this->current()->details

Example code below:

public function shortDescription()
    {
        if(strlen($this->current()->details) < 510){
            return strip_tags($this->current()->details);
        }
    }

Any help on helping me understand it will be greatly appreciated.

jamesjones21
jamesjones21
9,260 Points

Jason Anders could you possibly provide some help?

1 Answer

Hello James Jones,

I like to think of the -> as a chain/ a link going left to right.

So you understand $this, which refers to the object itself.

In the example below, when we call $this->property_name, we are saying go to $this, and then from $this access what we have stored in the "property_name" of $this. If you have experience with arrays, think of it as key -> value storage. So we read property_name of $this and set to my_property_value.

<?php
$my_property_value = $this->property_name

Since it is a chain/ link you can navigate further than one level as long as each value in chain is an object.

So for your example, reading left to right, we know what $this is. But then we call $this->current(). This half of the statement is saying call the "current" method on the object of $this, ie current object, and read the function's returned value. Which in your example the function current of $this return an object. Since it is an object we a chain to read it's properties, which in example is to read the details property.

Below example is the same out as your example, but without chaining and more broken into steps.

<?php
class Example {
public function shortDescription()
    {
        // Execute current method and gets it's returned object and set to $current
        $current = $this->current();
        // From the $current object, read its property of "details" and set to $details
        $details = $current->details;

        if(strlen($details) < 510){
            $details = strip_tags($details);
        }
        return $details;
    }
}

So we are not passing the property to the method. In order to pass a value to a method/ function, requires you to put it between the ().

This example below would be passing a property to a method.See how in $this->trimString($this->details); we are taking $this->details value and passing it to $this->trimString() method.

<?php
class Example {
private $details = '<p>Example details</p>';

public function getShortDescription()
    {
        return $this->trimString($this->details);
    }
protected function trimString($string)
    {
        if(strlen($string) < 510){
            $string = strip_tags($string);
        }
        return $string;
    }
}
jamesjones21
jamesjones21
9,260 Points

thanks very much, I did some further searching on method chaining and got some thorough learning through it so I am grateful for this.