0

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.

Update

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.

Update 2

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.

4
  • 1
    Having a static object which ends up having the same members as a single instance of a non-static counterpart will take the same memory. I'm not 100% sure if PHP does this but other languages basically have a special instance of each class which holds the static members so basically either you create a singleton or use static memory usage is the same. The difference is testability. If you use static testing is much harder, but this is probably not a concern you'd have for school. Commented Jan 20, 2018 at 20:18
  • we aren't even learning php at school, i'm learning this by myself, and thanks for your comment Commented Jan 20, 2018 at 20:20
  • @apokryfos can you explain your comment about the testability difference? I've found static methods much easier to test, since they are always self-contained, so there's no need to set any class variables or worry about states. Commented Jul 4, 2018 at 4:09
  • 1
    @BobRay there's more details in the phpunit page for test doubles but long story short if you're testing a unit which depends on another unit then you would normally mock the other unit to isolate your test, however most test frameworks will not mock statics, only create mock class instances. Commented Jul 4, 2018 at 5:32

1 Answer 1

2
+50

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.

  1. 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.

  2. 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:

  • specifying the __construct method as private, or
  • defining the whole class as 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.

Sign up to request clarification or add additional context in comments.

3 Comments

the processor class is not a proper class, but it call other proper class, instead of writing 10 lines of code in each function in index.php i decided to leave the process inside a static method in processor , does that make any difference ?
I can't really tell until I see your code in its entirety, but don't think it does in this case.
If a class contains only static methods and no class variables, wouldn't the amount of memory used by the instantiated class object be roughly the same as the memory required for "including" the class so you could call its static methods?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.