what is the -> operator?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm looking at some code and found the -> operator. In looking at the
PHP manual at php.net, I can't find a description of what it does. I
can find an example of where it is used, like on this page:

In the sample code like this:

class foo
    function do_foo()
        echo "Doing foo.";

$bar = new foo;

But what does it do?

The code that triggered this search was this code:

    function __construct()
        $this->soap_client = new SoapClient($this->wsdl);

In the above, is the $this equivalent to "this" in other languages,
ie. an instance of the object itself? Again, a search of the manual at
php.net did not yield anything for the $this variable.


Re: what is the -> operator?

Quoted text here. Click to load it

The part of the manual you must've missed is at:

$this is explained there in detail, and -> by means of many clear
examples. Indeed, $this is like "this" in many other languages. -> is
also like -> in e.g. c++, and does the same as the dot in e.g. java,
python and ruby. The reason it isn't simply the dot in PHP is probably
because the dot also means string concatenation.


Re: what is the -> operator?

Quoted text here. Click to load it

You're right, I did miss that part. I've now read it, and the
preceding PHP 4 Object section.

I was still confused, so then I found this site:

After reading that I started getting some clarification, but I'm still
a little lost.  Now my confusion extends to the double-colon and the
arrow operator.

If I have a class myClass with methods foo and bar

class MyClass{
  function foo() {}
  function bar() {}

then can I write

$thisVariable = myClass::foo  /*static call */


$classInstance = new MyClass;
$classInstance->foo /* call the foo method in my instance? */

Am I at least on the right track?  If there is a good book, about now
I'd be really glad to get a recommendation.


Re: what is the -> operator?

MikeB schreef:
Quoted text here. Click to load it


You can call a classmethod without instantiating the class first.
In that case we say it is a static call to that method (function).

That can be useful to the programmer to pack some functionality in a class.
For example, if you have a bunch of functions that manipulate strings in
some useful way, you can pack them together in a class; to organize your

If you use $this-> you must have an instantiated class, since the $this
points to that instantiation.

Erwin Moller

Re: what is the -> operator?

MikeB wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

Yes, you're on the right track.

-> is the object resolution operator. It applies to instances of objects.

:: is the namespace resolution operator. It applies to classes: static stuff
when you're outside a class, and the very class and its parents when you're
inside of a class. In PHP6, it'll be used to refer to namespaces too.

Quoted text here. Click to load it

Do read the PHP manual.

Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

OS/2: ¿Qué mitad deseas?

Site Timeline