471,624 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

self and $this usage


Is it possible to do without $this?

I have got an instance of another class inside a class. Now I want to access
this classes functions using the self keyword, but it seems i cant.

Or can I?

Like, self::$nexgen->Function?

Or do I have to use the $this keyword. Having both keywords seems pointless
to me, i'd like to stick with one...

Maybe im getting confused.
Jul 17 '05 #1
2 1661
Basically, self:: and $this apply to class that you are currently in.
So, if you have class A and it has a member variable that is an
instance of class B, self:: and $this refer to class A. To access
variables and functions of class B, the rules apply as they would
anywhere else. For instance:

Class A {
const someConst = 1;
var $b; //will be an instance of class B

function A() { //the constructor for class A
$b = new B();

function myFunc() { //execute some method in class B

static function myStaticFunc() {
echo "Hello!";

The difference between $this and self:: is as follows: use $this to
refer to class member variables and non-static methods. Use self:: to
refer to constants and static methods. The reason is that constants
and static methods don't require instantiation of an object, so $this
wouldn't exist (since $this refers to an instance of an object). So,
in the above example, to call myStaticFunc() from within class A, you
would write self::myStaticFunc(). Likewise, to use the constant
someConst, you would write self::someConst. To call myStaticFunc() or
use someConst from somewhere else (i.e. in a script or from some other
class), you would write A::myStaticFunc() or A::someConst (note that
it's 'A' and not the name of a variable that refers to an instance of
class A).

Jul 17 '05 #2
ZeldorBlat wrote:
Basically, self:: and $this apply to class that you are currently in.

I thought self refers the class of the the current object while $this refers
to the instance.

See also http://bugs.php.net/bug.php?id=30934

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Frans Englich | last post: by
15 posts views Thread by Ralf W. Grosse-Kunstleve | last post: by
20 posts views Thread by Wayne Sutton | last post: by
8 posts views Thread by RoSsIaCrIiLoIA | last post: by
4 posts views Thread by David Coffin | last post: by
3 posts views Thread by james_027 | last post: by
84 posts views Thread by braver | last post: by
18 posts views Thread by Gabriel Rossetti | last post: by
8 posts views Thread by ssecorp | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.