Welcome Guest, Not a member yet? Register   Sign In
Multiple Class Instances vs Function Call
#1

I have been pondering the wisdom of my methodology for a while and thought let's hear what the community has to say. I started this methodology using CI 3 and have now ported the principle to my CI4 applications
Scenario:
Developing a CRM type system and when designing the layout of the User Profile page I have to display various sections containing information from different parts of the system e.g. On the transactions tab we have two columns (1) table of all Invoices (2) table of Orders. Tab Support: Shows Tickets. Tab Technical: shows tables of Login History, Emails from the system etc.
Method 1:
At the start of the view create instances of all the Models that will be used to obtain the data e.g. InvoicesModel, OrderModel, TicketsModel etc. Then call the methods needed as and when required. E.g
Code:
<div>
<?= $modelInvoices->displayTable( $clientId); ?>
</div>
Rinse and repeat for all the information blocks. All the variables gets released once the view has been processed.

Method 2 (which I have been using):
I have created a helper function file which is loaded in BaseController. In this Wrapper Helper I have defined numerous functions that all they do is get an instance of a class and execute a method and return the result. To use the example above i Wrapper Helper:
PHP Code:
function getInvoiceTable$clientId) {
$model = new \App\Models\InvoicesModel();
return 
$model->displayInvoices$clientId);

In the view the code would look something like:
Code:
<div>
<?= getInvoiceTable( $clientId ) ?>
</div>

My thinking was that using the function call the model instance is created, used and the memory released whereas with method one the memory for all the class instances are only released on completion of the view.

The reason that I brought this up that in one app the profile view is now opening 7 model classes and that could increase going forward.

All thoughts and input will be appreciated.
Dirk B.
Abatrans Software
No SEO spam - see forum guidelines
Reply
#2

In CI4, you can use the model() function to get (or create) a shared instance of a model class.

If you benchmark Method 1 vs Method 2, there's probably not much in it and any difference probably won't be the main bottleneck in your application anyway.

One benefit of using Method 2 with functions for each one is that it is a good place to cache the results, eliminating the need to load the models, data from the database and whatever logic happens in displayInvoices().
Reply
#3

Like craig said, method 2 is better. But I would also add that calling the database from the view is not good practice. You may call it through a helper function that call the model class, but you are still making a DB request from your view. What I think is a better approach is to collect all the required data from the controller in a $data array and then pass this to the view to be displayed. That way you can wrap all the "logic" in a try/catch and display a proper error message in case something went wrong. If all goes well, your view only need to echo the data.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB