-2

I am studying how to connect database while learning PHP. Just a quick question. Does anyone can tell me what does "->" sign do in PHP? I cannot understand the functionality of this sign so that I have no idea how to edit the code. Thank whoever answer this.

MistyBreeze
  • 21
  • 1
  • 3

4 Answers4

3

-> Sign used in objects, to access it's property and methods.

class example{
    public $prop1 = 'Hello World';

    public function sayHello(){
        echo $this->prop1;
    }
}

$example = new example();
$example->sayHello();

Ref: Classes and Objects in PHP

kamal pal
  • 4,187
  • 5
  • 25
  • 40
2

-> Is Used to refer the Classes And Objects for more information check here.

Community
  • 1
  • 1
Sarvagna Mehta
  • 334
  • 3
  • 16
2

You haven't posted any code, so I'm not 100% sure where you saw this, but I'm almost certain you are referring to something like this:

$foo = new Foo();
echo $foo->bar;

In this example, -> is used to access a property of an object, $foo. It can also be used to access a method, as in $foo->baz();.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • tut, you should know this should just be closed and not answered ;-) –  Jun 11 '15 at 03:40
  • 2
    @Dagon It's certainly not a good, SO-worthy question. On the other hand, I'm sympathetic with OP just because it would be basically impossible for a true beginner to search for that syntax. Google, for example, wouldn't help much. – elixenide Jun 11 '15 at 03:42
  • well actually if you put the exact question title in Google you get a great answer –  Jun 11 '15 at 03:43
  • @Dagon Huh. I stand corrected. Google usually isn't a lot of help with programming syntax, so I just assumed it wouldn't be helpful here. – elixenide Jun 11 '15 at 03:49
  • @EdCottrell even in [php search](http://php.net/manual-lookup.php?pattern=->&scope=quickref) it returns `-> doesn't exist. Closest matches:` – YesItsMe Jun 11 '15 at 05:33
1

For real quick and dirty one-liner anonymous objects, just cast an associative array:

<?php

$obj = (object) array('foo' => 'bar', 'property' => 'value');

echo $obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'

?>

... no need to create a new class or function to accomplish it.

YesItsMe
  • 1,709
  • 16
  • 32