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