What is the cost of static and instance methods? - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, October 18, 2023

What is the cost of static and instance methods?

I'm planning to switch access of instantiated objects methods to static methods, but I'm not sure how to benchmark it properly and if the impact would be worse than it already is (in my opinion, you'll see.)

I've come in a situation that I do this for every route in the application I'm working on:

RoutesExample.php:

#[Routes('POST', '/somepage')]
public function doSomething()
{
   $serviceA = new ServiceA();
   return $serviceA->doSomething();
}

#[Routes('POST', '/otherpage')]
public function doAnotherThing()
{
   $serviceB = new ServiceB();
   return $serviceB->doAnotherThing();
}

And, by looking at it, it seems wrong. But I don't have enough experience to tell.

The idea I first thought was to set all services to Singletons, but I immediatly went back because Singletons holds the states in the memory. I believe I'd gain performance, but the memory side would be bad.

The main idea I thought was to use static methods in the service classes. But I don't have enough information about PHP 8.2 || PHP 8.X. I saw this question, but the maximum version of php there is 7.0. And I think a lot have changed until now.



source https://stackoverflow.com/questions/77311183/what-is-the-cost-of-static-and-instance-methods

No comments:

Post a Comment