Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

oh dear-

Name: Anonymous 2009-03-26 13:04

pardon my ignorance but,
class user{
private $user_id;
private $user_name;
public function __construct($user_id) {
$this -> user_id=$user_id;
$this -> user_name = $result['username'];

what is '$this ->'

Name: Anonymous 2009-03-26 17:26

>>1
This is PHP.
Regarding $this ->
$this is a predefined variable of every instance of any object in PHP and most OO languages will have something equivalent (cf "this" keyword in java etc..)

It simply behaves like a pointer to an object, namely itself; so you can treat it like a local variable of whatever type the class is; that contains a reference to itself.

Notice that all member variables of $this are the local varaibles of that object, variables of functions cannot be referenced by $this because they are not actual fields of the object but belong to the function call instead. Thus, a key usage for the $this variable is differentiating between two variables of the same name, where one is local to a method and one is a field of the object as is visible in your example. Sometimes in a poorly designed system $this can also be a quick hack to fix something that would of otherwise required a redesign; where there is a circular dependency and some created objects need to know what object instantiated them you could use Object obj = new Object(this)

Some languages will also allow the use of this() as a method/function. This esentially calls its own constructor as a method, and allows for overloaded constructors and other bits and peices. ex:
public class Jew {
    private int jewness;
    private ConcentrationCamp home;
    private boolean gassed;
    private Jew(ConcentrationCamp home, boolean gassed, int jewness) {
        this.home = home;
        this.gassed = gassed;
        this.jewness = jewness;
    }
    public Jew(boolean gassed) {
        this(ConcentrationCamp.AUSCHWITZ, gassed, 100);
    }
    public Jew() {
        this(true);
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List