473,390 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

self and $this usage

Hey

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 1719
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
$b->someMethod();
}

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

C.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Frans Englich | last post by:
Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
20
by: Wayne Sutton | last post by:
OK, I'm a newbie... I'm trying to learn Python & have had fun with it so far. But I'm having trouble following the many code examples with the object "self." Can someone explain this usage in...
8
by: RoSsIaCrIiLoIA | last post by:
Do you know how to write a self-checking program in standard C? Do I can think that if I write in a file.c static g="1234567"; in the file.exe (or file) there is in some place...
4
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but...
3
by: james_027 | last post by:
hi, is cls & self the same thing? I have seen something like class A: def dosomething(cls): #doing something
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.