To call a non-static method you need to instantiate the class (use the new
keyword to create a new instance of the class).
When calling a static method you don't have to "new it up" but it won't have direct access to any of the non-static properties or methods. There are dozens of use-cases / scenarios where you might want to use one over the other.
To be quite honest it has never even crossed my mind to think about performance of using one over the other. If it got to a point where it made that much of a noticeable difference (and all major steps had been taken to increase efficiency) then I would imagine that either the maintenance costs of such a big application would outweigh the need for the increased efficiency or the logic behind the app is fairly flawed to begin with.
Examples for static and non-static
If I was going to use a class for the example in your question then I would use the static version as nothing in the method is reliant on other properties of the class and you then don't have to instantiate it:
Session::start_new_session()
vs
$session = new Session();
$session->start_new_session();
Also, static properties on a class will remember state that would have otherwise been lost if you were to use a non-static property and instantiation:
class Session
{
protected static $sessionStarted = false;
static function start_new_session()
{
if (!static::$sessionStarted) {
session_start();
static::$sessionStarted = true;
}
}
}
Then even if you did:
$session = new Session();
$hasStated = $session::sessionStarted;
$hasStarted
would still be true
.
You could even do something like:
class Session
{
protected static $sessionStarted = false;
public function __construct()
{
$this->startIfNotStarted();
}
function startIfNotStarted()
{
if (!static::$sessionStarted) {
session_start();
static::$sessionStarted = true;
}
}
}
This way you don't need to worry about starting the session yourself as it will be started when you instantiate the class and it will only happen once.
This approach wouldn't be suitable if you had something like a Person
class though as the data would be different each time and you wouldn't want to use the same data in difference instantiations.
class Person
{
protected $firstname;
protected $lastname;
public function __construct($firstname, $lastname)
{
$this->firstname = $firstname;
$this->lastname = $lastname;
}
public function getFullname()
{
return "{$this->firstname} {$this->lastname}";
}
}
//
$person1 = new Person('John', 'Smith');
$person2 = new Person('Jane', 'Foster');
$person1->fullname(); // John Smith
$person2->fullname(); // Jane Smith
If you were to use static methods / properties for this class then you could only ever have one person
class Person
{
protected static $firstname;
protected static $lastname;
static function setName($firstname, $lastname)
{
static::$firstname = $firstname;
static::$lastname = $lastname;
}
static function getFullname()
{
return static::$firstname . ' ' . static::$lastname;
}
}
//
Person::setName('John', 'Smith');
Person::setName('Jane', 'Foster');
Person::getFullname(); //Jane Foster
The debates
You are probably going to see a lot of debates over which is better and wha the best practices are when it comes to PHP (not just about static and not-static methods).
I wouldn't get bogged down with it though! If you find that one side makes more sense to you (and whatever you building at the time) then go with that one. Standards and opinions change all the time in this community and half of the stuff that was a problem 5 years ago (or even less) will actually be non-issues today.
Take the Laravel
framework as an example -- one of the (many) debates is that Facades are bad because they're static and make use of magic methods which is hard to test and debug. Facades are actually very easy to test and with the use of stack trace error reporting it isn't hard to debug at all.
That being said, if you find the majority of people are saying the same thing and there isn't really a debate for the other side, there is probably a reason e.g. don't use mysql_
functions or don't run queries inside a loops.
Hope this helps!