Jump to content

Best practice: self::method() or $this->method()


silkfire

Recommended Posts

I've been thinking about this for a while. In my classes I use self::method() for all methods, including non-static ones. Why does PHP allow this in the first place?

 

What are best coding practices do you think? I haven't been able to find much about this by googling except for this but it didn't give me an intelligent answer.

 

http://forums.devnetwork.net/viewtopic.php?f=1&t=113624

Link to comment
Share on other sites

It's a loosely-typed language.

 

Since an 'instance' will always exist within the class (the methods will always be available), there's no reason to differentiate.

 

Personally, I'll use $this->whatever for all internal references... just because?

 

I guess the 'better' one to use is the one your team is using, or the one in the rest of your code :)

Link to comment
Share on other sites

It is a common misconception that self:: is only for static methods, and $this is only for non-static methods. This isn't entirely true, though, and both can be used in both ways.

 

self:: refers to the current class, and $this refers to the current object.

 

Consider this example:

<?php

class Car
{
public function getMake()
{
	return 'Ford';
}

public function getModel()
{
	return 'Focus';
}

public function getVehicle()
{
	return $this->getMake() . ' ' . $this->getModel();
}
}

class Truck extends Car
{
public function getModel()
{
	return 'F150';
}
}

 

If we create a new Truck object, and call the "getVehicle" method, it will output "Ford F150".

<?php
$truck = new Truck;
echo $truck->getVehicle(); // Ford F150

 

It does so because $this refers to the current object, which is Truck.

 

If we change $this->getModel() to self::getModel(), it will use the current class's (current being the class that self:: resides in, in this case Car) getModel method.

 

<?php
...
public function getVehicle()
{
return $this->getMake() . ' ' . self::getModel();
}
...

$truck = new Truck;
echo $truck->getVehicle(); // Ford Focus

 

I hope that severely lackluster example helps you understand.

Link to comment
Share on other sites

It's also worth noting that when in a static context, static:: can sometimes be superior to self:: It implements late-static-binding. In a nut shell, that means that static:: works on the same idea as $this, in that it refers to the class in which it was called and not in which it resides (for lack of better terminology).

 

So, in my earlier example if you changed self::getModel() to static::getModel(), you would end up with "Ford F150", just like with $this->getModel().

 

In addition to that, it makes unit testing a lot easier for static methods.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.