Yii Framework Forum: Scope Resolution Operator (::) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Scope Resolution Operator (::) Rate Topic: -----

#1 User is offline   Giovanni D. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 236
  • Joined: 23-December 08
  • Location:London (UK)

Posted 21 May 2009 - 09:26 AM

Hi,
I've learned php by myself and since some time ago I was focused only to version 4 cause I were woking on some projects that had to be php4 compatible.

Now this is over and I'm enjoying php5 and yii, but a couple of things about Classes and Objects are not 100% clear to me..

I know a bit OOP also thanks to java and was already working with classes also with php4.. but for example just yesterday one question about the scope resolution operator camed in my mind..

Now from the documentation ( http://www.php.net/m...nekudotayim.php ) I thik to have the answer, but would like to share my thoughts with you so you can confirm this to me ;)

If I've well understood, the meaning of this operator is to access to some methods and constants of an object without having to create an instance of it.. works a bit like a singleton but you don't even have to assign it to a variable (=create the first instance and a pointer)

So.. I tried to use "$this->anotherMethod()" inside a method I've called with MyClass::method() but I saw that it were trying to find it out as a method of my "parent" class.. so I think in that case I had to use "self::anotherMethod()" .. and that"$this->anotherMethod()" were the same of "parent::anotherMethod()" .. am I right ? (maybe not: cause problably parent refers only to the constant / methods that are inherit from the parent class when you extend a class.. ::) )

I've also saw that if I creat an instance of the object, like:
$my_class =new MyClass();

and then I call "$my_class->method()" the "$this->anotherMethod()" call will look for a method named "anotherMethod" inside the $my_class object, as expected.


So.. in which cases do you use the Scope Resolution Operator approach? And is it safe to rely on "$this" when you want your class to interact with the class that has called the static method of a class?

From the comment of "luka8088 at gmail dot com" it seems that if you use $this you get a strict warning..  ::)

thanks.

bye,
Giovanni.
0

#2 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 02 June 2009 - 05:38 PM

Hi,
well, $this is a pseudo-variable that is only available when you call a method from within an object. $this afterwards refers to the the calling object(!). So there is no need that it refers to the object the called method belongs to (in most cases it does, because you use the object the method belongs to to call the method), but if you use $this within a method you called statically than $this refers to the calling object and not to the called class or something like this.
Using self::anotherMethod() inside a class is nearly the same as using MyClass::anotherMethod() from the outside.

I don't know how you did your observations, but e.g. if you called MyClass::method() from within an object of MyClassParent than $this in MyClass::method() points to the parents method().

So you don't really know in which objects context your static method is called and therefore you could not be sure that $this refers to the object you're expecting.

Greets
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users