The question is in the name really...
Whats the different between '$this' and 'self::' ??The answer is in the static resolution operator '::' self refers to the current class, $this refers to the current object.
To expand:
Classes effectively serve as templates for creating (instantiating) objects. Both classes and objects can have properties and methods.
Class properties and methods are declared in the class definition with the "static" keyword (which is why they're often called "static properties" etc.). They don't belong to the objects that are created; the class keeps them to itself, instead of recreating them over and over for each instance object.
The upshot of this is that if an object manipulates one of its class variables, all the other objects know about it, because they're all using the same variable.
A class might have a class variable that keeps count of how many instances have been created so far (for the purposes of, e.g., assigning a unique identifier to each).
class foo
{
private static $num_created=0;
private $uid;
function __construct()
{
self::$num_created++;
$this->uid = self::$num_created;
}
}
The first foo that is constructed will get a uid of 1, the second that is constructed will get a uid of 2, and so on. That's because all of these foo objects will be looking at the same $num_created variable, and each of them has their own distinct $uid variable.
Static methods are convenient for internal utility methods (like comparators for usort()), factory methods (that provide conventionalised access to the constructor) - in general, methods which are relevant to the class as a whole, but not necessarily to any particular instance of that class.
To bring all that back to the point of the question. Class (static) methods and properties are referenced by the classname:: syntax, while object methods and properties are referenced by the $objectname-> syntax. Within a method, the current class is called "self", and the current object is called "$this".
It makes no sense to use "$this" in a class method, because - since that method is shared by all objects - there is no specific object for $this to refer to. PHP will whine about it if you try. You are free to use "self::" in an object method, since every object does belong to a specific class.My answer hangs it's head in shame and walks off, over here --->Note to $this-> don't take so long to reply (at least, not without refreshing first).thanks for the detailed reply, it makes sense now.
cheersHi.
Sorry to disturb you.
Excelent explanation Weedpacket.
This is a little OT but ............
In your opinion it makes sense
a class with static method like this
for instance (It belongs to a
general purpose class to manage string)
public static function dateTransform($key,$value,$findMe){
if(strpos($key, $findMe)!==false){
if(is_numeric($value)){
$value=date("d-m-Y",$value);
}
}
return $value;
}
Thanks in advance.
Bye.
Whats the different between '$this' and 'self::' ??The answer is in the static resolution operator '::' self refers to the current class, $this refers to the current object.
To expand:
Classes effectively serve as templates for creating (instantiating) objects. Both classes and objects can have properties and methods.
Class properties and methods are declared in the class definition with the "static" keyword (which is why they're often called "static properties" etc.). They don't belong to the objects that are created; the class keeps them to itself, instead of recreating them over and over for each instance object.
The upshot of this is that if an object manipulates one of its class variables, all the other objects know about it, because they're all using the same variable.
A class might have a class variable that keeps count of how many instances have been created so far (for the purposes of, e.g., assigning a unique identifier to each).
class foo
{
private static $num_created=0;
private $uid;
function __construct()
{
self::$num_created++;
$this->uid = self::$num_created;
}
}
The first foo that is constructed will get a uid of 1, the second that is constructed will get a uid of 2, and so on. That's because all of these foo objects will be looking at the same $num_created variable, and each of them has their own distinct $uid variable.
Static methods are convenient for internal utility methods (like comparators for usort()), factory methods (that provide conventionalised access to the constructor) - in general, methods which are relevant to the class as a whole, but not necessarily to any particular instance of that class.
To bring all that back to the point of the question. Class (static) methods and properties are referenced by the classname:: syntax, while object methods and properties are referenced by the $objectname-> syntax. Within a method, the current class is called "self", and the current object is called "$this".
It makes no sense to use "$this" in a class method, because - since that method is shared by all objects - there is no specific object for $this to refer to. PHP will whine about it if you try. You are free to use "self::" in an object method, since every object does belong to a specific class.My answer hangs it's head in shame and walks off, over here --->Note to $this-> don't take so long to reply (at least, not without refreshing first).thanks for the detailed reply, it makes sense now.
cheersHi.
Sorry to disturb you.
Excelent explanation Weedpacket.
This is a little OT but ............
In your opinion it makes sense
a class with static method like this
for instance (It belongs to a
general purpose class to manage string)
public static function dateTransform($key,$value,$findMe){
if(strpos($key, $findMe)!==false){
if(is_numeric($value)){
$value=date("d-m-Y",$value);
}
}
return $value;
}
Thanks in advance.
Bye.