Reputation: 1363
I am currently learning object oriented concepts in PHP. and I found some examples for getter and setter methods:
example1:
class UserModel{
private $id,$name,$email;
public function __construct(){}
public function setid($id){
$this->id=$id;
}
public function getid(){
return $this->id;
}
public function setname($name){
$this->name=$name;
}
public function getname(){
return $this->name;
}
public function setemail($email) {
$this->email=$email;
}
public function getemail() {
return $this->email;
}
}
example2:
class UserModel{
private $id,$name,$email;
public function __construct(){}
public function setid($id){
$this->id=$id;
}
public function getid(){
return $id;
}
public function setname($name){
$this->name=$name;
}
public function getname(){
return $name;
}
public function setemail($email) {
$this->email=$email;
}
public function getemail() {
return $email;
}
}
As you can notice, in first example, in getter methods, $this->property
is returned. and in second, $property
is returned.
From manual, I knew about $this that it will always have reference of invoking object, so in this case, $this->property
will ensure that it will return only property of invoking object. But, as in first example, return $property
should also have the reference of invoking object.
For example if I write:
UserModel $user = new UserModel();
$user->setemail("[email protected]");
$user->getemail();
then, in both examples, getter method will always have reference of UserModel object. So, are they both same or different? if they are different, then what is the difference between them? Can anyone explain if both work differently in different scenarios?
Sorry but I'm new to OOP. please help guys.
Upvotes: 1
Views: 808
Reputation: 214959
When you're using a name (identifier) in your program, the compiler/interpreter needs to obtain its value. To do that, it consults its internal table of name=>value pairs, called "scope". If the name is not found in the current scope, it consults another scope, if possible, until there are no more scopes to look in - in which case, it shouts at you complaining that the name is undefined. This process is called "name resolution" and scopes are linked together in a "scope chain".
I don't know Java, but I think its name resolution looks like this:
foo
, ...So when you write foo
in your code, and there's no local variable foo
, Java will look for a field named foo
.
PHP's resolution rules are different and much simpler:
$foo
...global
As you can see, class declaration is not included in the scope chain, therefore $foo
will never refer to a field - it's always a variable. That's why you always have to use explicit $this->
when referring to object fields.
PHP is not unique in this regard - other scripting languages use explicit this
too (self.foo
, @foo
) and the reason for this is that these languages don't have declarations, so when you have foo=1
it would be impossible for the compiler to decide if you're assigning a new value to a field or introducing a new local variable.
Upvotes: 1
Reputation: 25
Simply it's a way to reference variables in the object. The variables you create in body of class are called attributes of object and to access them you need to reference the object they belong to. Since you don't know what object variable will be called that's why you need to use $this->variable where $this is a pseudo-variable.
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
Then there are local variables you can use only in body of function.
class Test {
private $attribute; //This attribute can be used in methods by using $this->attribute
function testFunction() {
$local; //This variable can be used only in this function
}
}
And about your question. In your example 2 of UserModel the method getemail() returns local variable which is fine as long as it's defined, but won't return attribute $email.
Upvotes: 0
Reputation: 802
"This" is a reference to the variable of "this" class you are working on.
So, return $something will not work for you. It will return null. While $this->something will return the value of the variable of this class.
Upvotes: 0
Reputation: 2769
There is a slight difference in the two statements:
$this->$something;
The above statement means that a variable i.e $something is returned that is the part of the object of the current class. &this
in the statement means the current pointer or reference of the current class and the variable in that reference is returned.
$something
In the above statement a normal variable containing any value is used.
Upvotes: 1
Reputation: 3363
In your second example, return $email;
returns an undefined variable, or in other words: that will not work. The reverence to the UserModel object is the $this
variable. You always need to use $this
if you want to access a class method or property.
Upvotes: 1