0

I'm looking at some code here and there are bunch of calls that are like this. I don't have much experience with php, mainly java. Is this like if I had a Dog object that had a fur object and I wanted to call "dog.getFur().getColor();" ?

Example:

  $this->tbtemp_list1->lead_time = ($this->add_lead_time->Text + $this->add_transit_time->Text);
  $this->tbtemp_list1->units = $this->add_units->Text;
  $this->tbtemp_list1->item_class = $this->txtClass->Value;
  $this->tbtemp_list1->category = $this->add_part_category->Text;
  $this->tbtemp_list1->description = $this->add_part_description->Text;
  $this->tbtemp_list1->notes = $this->txtNotes->Text;
davidahines
  • 3,586
  • 15
  • 49
  • 85
  • You are accessing a sub-object. So the first line `$this->tbtemp_list1` is an object, and you are accessing the `lead_time` property of that object. – DaveRandom Jan 17 '12 at 18:15
  • *(related)* http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Gordon Jan 17 '12 at 18:16

4 Answers4

2

You are right in your thinking, the -> operator simply accesses a member of an object, such that in $this->tbtemp_list1->lead_time the tbtemp_list1 member of the current object is being accessed, and then the lead_time member of the tbtemp_list1 member.

James Shuttler
  • 1,294
  • 9
  • 15
2

in php

a->b->c is accessing c which is a property or method of b, and b is a child object of a. In javascript, you're just method chaining.

Crayon Violent
  • 30,524
  • 3
  • 51
  • 74
1

Is this like if I had a Dog object that had a fur object and I wanted to call "dog.getFur().getColor();" ?

Yes. The "->" operator in PHP is similar to the "->" operator in C.

In C, it gets a member from a pointer. In PHP, it gets a member from a reference. Since PHP is written in C, I suspect that the syntax for accessing members from objects was directly borrowed from C.

Edit:
It's also good to note here that static members require a different syntax.

print className::$staticVariable;
ken
  • 16,047
  • 3
  • 46
  • 70
1

-> (arrow) is object notation in PHP.

It's equivalent is the . (dot) object notation Java.

For example:

// PHP
$this->tbtemp_list1->lead_time

is:

// Java
this.tbtemp_list1.lead_time

In this case, you are reference a property (lead_time) that is a property of an object lead_time which is a property of an object ($this). This can go on forever provided the property is an object.

Jason McCreary
  • 66,624
  • 20
  • 123
  • 167