Click here to get back home

what is the -> operator?

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
what is the -> operator? MikeB 06-01-2008
Get Chitika Premium
Posted by MikeB on June 1, 2008, 11:45 am
Please log in for more thread options
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:
http://us2.php.net/manual/en/language.types.object.php

In the sample code like this:

<?php
class foo
{
function do_foo()
{
echo "Doing foo.";
}
}

$bar = new foo;
$bar->do_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.

Thanks.

Posted by Egbert Teeselink on June 1, 2008, 12:15 pm
Please log in for more thread options
> 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:http://us2.php.=
net/manual/en/language.types.object.php
>
> In the sample code like this:
>
> <?php
> class foo
> {
> =A0 =A0 function do_foo()
> =A0 =A0 {
> =A0 =A0 =A0 =A0 echo "Doing foo.";
> =A0 =A0 }
>
> }
>
> $bar =3D new foo;
> $bar->do_foo();
> ?>
>
> But what does it do?
>
> The code that triggered this search was this code:
>
> =A0 =A0 function __construct()
> =A0 =A0 {
> =A0 =A0 =A0 =A0 $this->soap_client =3D new SoapClient($this->wsdl);
> =A0 =A0 }
>
> 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.
>
> Thanks.

The part of the manual you must've missed is at:
http://us2.php.net/manual/en/language.oop5.php

$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.

-egbert

Posted by MikeB on June 1, 2008, 1:27 pm
Please log in for more thread options
>
>
>
> > 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:http://us2.php.net/manual/en/language.types.object.php
>
> > In the sample code like this:
>
> > <?php
> > class foo
> > {
> > function do_foo()
> > {
> > echo "Doing foo.";
> > }
>
> > }
>
> > $bar = new foo;
> > $bar->do_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.
>
> > Thanks.
>
> The part of the manual you must've missed is
at:http://us2.php.net/manual/en/language.oop5.php
>
> $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.
>
> -egbert

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:
http://www.sitepoint.com/article/object-oriented-php

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 */

and

$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.

Thanks

Posted by Erwin Moller on June 2, 2008, 6:15 am
Please log in for more thread options
MikeB schreef:
>>
>>
>>
>>> 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:http://us2.php.net/manual/en/language.types.object.php
>>> In the sample code like this:
>>> <?php
>>> class foo
>>> {
>>> function do_foo()
>>> {
>>> echo "Doing foo.";
>>> }
>>> }
>>> $bar = new foo;
>>> $bar->do_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.
>>> Thanks.
>> The part of the manual you must've missed is
at:http://us2.php.net/manual/en/language.oop5.php
>>
>> $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.
>>
>> -egbert
>
> 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:
> http://www.sitepoint.com/article/object-oriented-php
>
> 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 */
>
> and
>
> $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.
>
> Thanks

Hi,

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
code.

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

Regards,
Erwin Moller

Posted by =?ISO-8859-15?Q?Iv=E1n_S=E1nch on June 2, 2008, 7:38 am
Please log in for more thread options
MikeB wrote:
[...]
> 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.
[...]
> Am I at least on the right track?

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.

> If there is a good book, about now

Do read the PHP manual.

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

OS/2: ¿Qué mitad deseas?

Similar ThreadsPosted
what does this operator do -> October 10, 2006, 9:51 pm
Operator Overloading July 14, 2006, 12:17 pm
prepend operator? June 18, 2008, 2:25 pm
Variable assignment using OR operator? February 22, 2005, 3:43 pm
turning execution operator on August 14, 2007, 4:50 pm
Learning PHP: Problem with Ternary Operator September 14, 2004, 12:38 pm
Re: Learning PHP: Problem with Ternary Operator September 14, 2004, 5:56 am
ternary operator or if() statement for default value? October 9, 2008, 3:20 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap