I have made my own php mini-framework and my index.php looks something like this
<?php
require __DIR__.'/../app/autoloader.php';
Autoloader::register();
use Stories\http\routing\{RouteCollection,Matcher,Route};
use Stories\http\Request;
use Stories\App\Stories;
$app = new Stories();
$routes = new RouteCollection();
$home = new Route('/',function(Request $request) use ($app) {
return \Stories\Processor::home($request,$app);
}, ['method' => 'GET']);
$routes->add("home",$home);
...
$login = new Route('/login', function (Request $request) use ($app) {
return \Stories\Processor::login($request,$app);
}, ['method' => 'GET']);
$routes->add('login',$login);
$matcher = new Matcher($routes);
$matcher->find()->run();
However, some people told me that making an instance of \\my\\namespace\\processor
and passing it to the function like this :
<?php
require __DIR__.'/../app/autoloader.php';
Autoloader::register();
use Stories\http\routing\{RouteCollection,Matcher,Route};
use Stories\http\Request;
use Stories\App\Stories;
use Stories\Processor;
$app = new Stories();
$routes = new RouteCollection();
$processor = new Processor();
$home = new Route('/',function(Request $request) use ($app,$processor) {
return $processor->home($request,$app);
}, ['method' => 'GET']);
$routes->add("home",$home);
...
$login = new Route('/login', function (Request $request) use ($app,$processor) {
return $processor->login($request,$app);
}, ['method' => 'GET']);
$routes->add('login',$login);
$matcher = new Matcher($routes);
$matcher->find()->run();
Would be better since the class instance have been created, and I don't know why, my router doesn't run any function but only the one that matches the URI so I don't think it's going to be a problem. I have tested both ways and I don't see any difference.
Since I'm a high school student and the one of the people who told me this is a teacher at a university I though that he may be right, but I don't see any difference in memory usage.
Based on @smuuf's answer, I want to say that processor
is not a proper class, instead of writing 10 or 20 lines of code inside every function in index.php calling other classes like view
, users
, orm
... etc, I made the processor
class with static methods that does exactly the same, the processor
class doesn't use any of its own methods or properties while processing the request.
I just updated my router and I also updated the code to look exactly like my index.php but I'm still looking for answer for the same question.
Short answer: It doesn't matter.
Longer answer: There's nothing inherently better about passing an instance around, if that instance is created unnecessarily. See, it all boils down to what your processor
really does .
If the processor
really is some complex processing component that needs to hold some internal state between calls to its methods, or share some resources (eg. a database connection), then yes, that is a "proper class" and absolutely should be instantiated.
If your processor
is really only a "collection of related (or unrelated) functions" , where (for example) each method handles a different URL in your app (as in your example) , and the instance wouldn't need to deal with some internal state, and its methods wouldn't share any resources (eg. a database connection), there's really no need to create an instance for that.
In fact, if I wrote such "helper/function collection class", I would explicitly restrict instantiation by either:
__construct
method as private
, or abstract class
. Ultimately, this is more about how you want to organize your code. The memory usage difference is - in this case - negligible.
In my opinion (and strictly speaking) : Using static methods would indeed need less memory and would be slightly faster, since no one needs to deal with instantiating the class prior to calling its methods. However, such difference in performance would be unnoticeable in 99 % of cases in the real world applications.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.